熊猫:编码行值/变换数据帧
问题描述:
这与this answer有些类似,但我的问题有点不同。熊猫:编码行值/变换数据帧
我的数据框:
df=pd.DataFrame([{'date': '2017-01-01', 'id': 'google', 'category': 1, 'Score1': 51, 'Score2': 531},
{'date': '2017-01-01', 'id': 'google', 'category': 2, 'Score1': 592, 'Score2': 152},
{'date': '2017-01-01', 'id': 'google', 'category': 5, 'Score1': 55, 'Score2': 255},
{'date': '2017-01-01', 'id': 'yahoo', 'category': 7, 'Score1': 597, 'Score2': 357},
{'date': '2017-01-01', 'id': 'yahoo', 'category': 8, 'Score1': 58, 'Score2': 58},
{'date': '2017-01-02', 'id': 'google', 'category': 5, 'Score1': 795, 'Score2': 455},
{'date': '2017-01-02', 'id': 'google', 'category': 1, 'Score1': 71, 'Score2': 751},
{'date': '2017-01-02', 'id': 'google', 'category': 2, 'Score1': 792, 'Score2': 352},
{'date': '2017-01-02', 'id': 'yahoo', 'category': 7, 'Score1': 77, 'Score2': 957},
{'date': '2017-01-02', 'id': 'yahoo', 'category': 8, 'Score1': 798, 'Score2': 358}
])
,看起来像这样:
date id category Score1 Score2
0 2017-01-01 google 1 51 531
1 2017-01-01 google 2 592 152
2 2017-01-01 google 5 55 255
3 2017-01-01 yahoo 7 597 357
4 2017-01-01 yahoo 8 58 58
5 2017-01-02 google 5 795 455
6 2017-01-02 google 1 71 751
7 2017-01-02 google 2 792 352
8 2017-01-02 yahoo 7 77 957
9 2017-01-02 yahoo 8 798 358
我需要转换成数据帧,看起来像这样的:
date id cat1_score1 cat2_score1 cat5_score1 cat7_score1 cat8_score1 cat1_score2 cat2_score2 cat5_score2 cat7_score2 cat8_score2
1/1/17 google 51 592 55 0 0 531 152 255 0 0
1/1/17 yahoo 0 0 0 597 58 0 0 0 357 58
1/2/17 google 71 792 795 0 0 751 352 455 0 0
1/2/17 yahoo 0 0 0 77 798 0 0 0 957 358
需要说明这里是类别的数量可以从id
到id
不等。还可能有一个需要考虑的辅助ID列。 我可能enumerate
在id
列的值,但然后我将如何转换数据帧相应?
答
让我们重塑这样的:
df_out = df.set_index(['date', 'id', 'category']).stack().unstack([2, 3]).fillna(0)
df_out.columns = df_out.columns.map('{0[0]}_{0[1]}'.format)
df_out = df_out.add_prefix('cat').reset_index()
print(df_out)
输出:
date id cat1_Score1 cat1_Score2 cat2_Score1 cat2_Score2 \
0 2017-01-01 google 51.0 531.0 592.0 152.0
1 2017-01-01 yahoo 0.0 0.0 0.0 0.0
2 2017-01-02 google 71.0 751.0 792.0 352.0
3 2017-01-02 yahoo 0.0 0.0 0.0 0.0
cat5_Score1 cat5_Score2 cat7_Score1 cat7_Score2 cat8_Score1 \
0 55.0 255.0 0.0 0.0 0.0
1 0.0 0.0 597.0 357.0 58.0
2 795.0 455.0 0.0 0.0 0.0
3 0.0 0.0 77.0 957.0 798.0
cat8_Score2
0 0.0
1 58.0
2 0.0
3 358.0
说明:在一切,但得分列首先,然后用stack
垂直对齐所有得分。接下来,使用unstack
将分类和分数转换为列,并将fillna
转为零。使用列map
将列级别组合到一个级别,并将“猫”列组合到列中。最后,reset_index
将所有列从索引中取回到数据框中。
感谢@Scott - 这似乎已从数据帧列中删除了'date'和'id',尽管 – Craig
您可以reset_index结束以将其返回到数据框。 –
@克雷格更新.... –