[英]Pandas pivot table error
Following code gives 以下代码给出
Code: 码:
table_channel = pd.pivot_table(data=df,values = 'Category',index =
['ID'], aggfunc='count')
Output: 输出:
ID Category
1 2
2 11
3 5
4 3
Right now it's giving total count of different categories of Category column. 现在,它给出了“类别”列的不同类别的总数。 Need output like: 需要如下输出:
ID Category1 Category2 Category3
1 0 1 1
2 5 4 2 and so on
I used this code to rectify but it did not work: 我使用以下代码进行纠正,但没有成功:
table_channel = pd.pivot_table(df,values = 'Category',columns = 'Category',index = ['ID'], aggfunc='count')
Error is that grouper for Category not 1 dimensional. 错误是类别的分类器不是一维的。 What is wrong? 怎么了?
You can using crosstab
您可以使用crosstab
pd.crosstab(df.ID,df.Category).add_prefix('Category')
Out[1335]:
Category Category2 Category3 Category5 Category11
ID
1 1 0 0 0
2 0 0 0 1
3 0 0 1 0
4 0 1 0 0
需要改用以下代码:
columns = df.Category.values
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.