[英]getting pivot table in pandas in the desired format
I have following dataframe我有关注 dataframe
Categories Owner
Product_Query products_team
Product_Query products_team
Infrastructure_Query infra_team
Bug_Query development_team
Bug_Query development_team
I want to display the above data in the following format我想以如下格式显示上述数据
Categories Total products_team infra_team development_team
Product_Query 2 2 0 0
Infrastructure_Query 1 0 1 0
Bug_Query 2 0 0 2
I am doing the following我正在做以下事情
df["count"]=1
df_pivot=df.pivot_table(index=["Catagories"],columns=["Owner"],values=["count"],aggfunc=[np.sum],fill_value=0)
But I am not getting the output in the correct format.How can I get the output in the format mentioned above?但是我没有得到正确格式的 output。我怎样才能得到上述格式的 output?
pd.crosstab
:pd.crosstab
:pd.crosstab(df['Categories'], df['Owner'], margins=True, margins_name='Total')
Output: Output:
Owner development_team infra_team products_team Total
Categories
Bug_Query 2 0 0 2
Infrastructure_Query 0 1 0 1
Product_Query 0 0 2 2
Total 2 1 2 5
And, if you don't want the row Total you can drop it:而且,如果您不想要 Total 行,您可以删除它:
df_out = pd.crosstab(df['Categories'], df['Owner'], margins=True, margins_name='Total')
df_out = df_out.drop('Total')
df_out
Output: Output:
Owner development_team infra_team products_team Total
Categories
Bug_Query 2 0 0 2
Infrastructure_Query 0 1 0 1
Product_Query 0 0 2 2
pivot_table
:pivot_table
:df.assign(counts=1)\
.pivot_table('counts','Categories','Owner','sum',
fill_value=0, margins=True, margins_name='Total')\
.drop('Total')
Output: Output:
Owner development_team infra_team products_team Total
Categories
Bug_Query 2 0 0 2
Infrastructure_Query 0 1 0 1
Product_Query 0 0 2 2
groupby
:groupby
:df_out = df.groupby(['Categories', 'Owner'])['Owner'].count().unstack(fill_value=0)
df_out['Total'] = df_out.sum(1)
Output: Output:
Owner development_team infra_team products_team Total
Categories
Bug_Query 2 0 0 2
Infrastructure_Query 0 1 0 1
Product_Query 0 0 2 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.