[英]How to extend pandas Dataframe by values in multiindex column?
I have a table with Multiindex getting by groupby():我有一张由 groupby() 获取的 Multiindex 表:
new_data=data.groupby(['id','category']).sum('amount')
new_data
Output looks like that (just a random table with the same form as mine): Output 看起来像这样(只是一个与我的表格相同的随机表):
# amount
#id category
1 2 12
3 1
2 1 45
3 56
Unique Id has some amount in each category. Unique Id 在每个类别中都有一定数量。
But I need to get a table where is amount presented for each 'id' for each category in the same columns.但我需要得到一个表,其中显示了同一列中每个类别的每个“id”的数量。
Something like that:像这样的东西:
#id amount_category_1 amount_category_2 amount_category_3
1 0 12 1
2 45 0 56
Assuming that you reset the index of your dataframe and have it in the form:假设您重置了 dataframe 的索引并采用以下形式:
id category amount
0 1 1 45
1 1 3 34
2 2 2 36
3 2 3 24
you can pivot your dataframe and rename your columns:你可以 pivot 你的 dataframe 并重命名你的列:
df = df.pivot_table(index='id',columns='category',values='amount').rename_axis(None, axis=1).reset_index()
df = df.rename(columns={c: 'category_'+ str(c) for c in df.columns if c not in ['id']})
to get somethng like this:得到这样的东西:
id category_1 category_2 category_3
0 1 45.0 NaN 34.0
1 2 NaN 36.0 24.0
Change NaN
using df.fillna(0)
to get使用df.fillna(0)
改变NaN
得到
id category_1 category_2 category_3
0 1 45.0 0.0 34.0
1 2 0.0 36.0 24.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.