英文:
How to use groupby for multiple columns in pandas for the below shown image?
问题
这是在pandas中的输入表格:
[![在这里输入图片描述][1]][1]
[1]: https://i.stack.imgur.com/Du7js.png
这是如下所示的输出表格:
[![在这里输入图片描述][2]][2]
[2]: https://i.stack.imgur.com/3GZuK.png
dtype: int64
亲爱的朋友们,
我是pandas的新手,如何使用pandas获得第二张图片中显示的结果。
我使用以下方法得到如下输出:
"df.groupby(['Months', 'Status']).size()"
Months  Status
Apr-20  IW        2
        OW        1
Jun-20  IW        4
        OW        4
May-20  IW        3
        OW        2
dtype: int64
但如何将这个输出转换为第二张图片所示的样式呢?
如果有人能帮助我,将会更有帮助。提前感谢。
英文:
<pre><br>  This is input table in pandas:
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/Du7js.png
<br>
this is an output table as shown below:
[![enter image description here][2]][2]
[2]: https://i.stack.imgur.com/3GZuK.png
dtype: int64
<br>
<br>
Dear Friends,
<br>
I am new to pandas, how to get the result is shown in the second image  using pandas.
I am getting output as shown below using this approach <br>
"df.groupby(['Months', 'Status']).size()"
<br>
Months  Status
<br>
Apr-20  IW        2
<br>
OW        1
<br>
Jun-20  IW        4
<br>
OW        4
<br>
May-20  IW        3
<br>
OW        2
<br>
dtype: int64
<br>
But how to convert this output as shown in the second image?
It will be more helpful if someone is able to help me.  Thanks in advance.
<br>
答案1
得分: 2
使用crosstab函数,带有margins=True参数,然后如果需要,移除最后的Total列,通过DataFrame.reindex按照原始列的顺序重新排列列,最后通过DataFrame.reset_index将索引转换为列,并通过DataFrame.rename_axis移除列名:
df = (pd.crosstab(df['Status'], df['Months'], margins_name='Total', margins=True)
       .iloc[:, :-1]
       .reindex(df['Months'].unique(), axis=1)
       .reset_index()
       .rename_axis(None, axis=1))
print (df)
  Status  Apr_20  May_20  Jun_20
0     IW       4       2       4
1     OW       1       2       4
2  Total       5       4       8
英文:
Use crosstab with margins=True parameter, then if necessary remove last Total column, change order of columns by DataFrame.reindex with ordering of original column and last convert index to column by DataFrame.reset_index and remove columns names by DataFrame.rename_axis:
df = (pd.crosstab(df['Status'], df['Months'],  margins_name='Total', margins=True)
       .iloc[:, :-1]
       .reindex(df['Months'].unique(), axis=1)
       .reset_index()
       .rename_axis(None, axis=1))
print (df)
  Status  Apr_20  May_20  Jun_20
0     IW       4       2       4
1     OW       1       2       4
2  Total       5       4       8
答案2
得分: 1
Unstack,然后转置:
df = df.groupby(['Months', 'Status']).size().unstack().T
获得一个 total 行:
df.sum().rename('Total').to_frame().T.append(df)
英文:
Unstack, and then transpose:
df = df.groupby(['Months', 'Status']).size().unstack().T
To get a total row:
df.sum().rename('Total').to_frame().T.append(df)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论