How to groupby two columns and calculate the summation of rows using Pandas?

谁说我不能喝 提交于 2021-02-10 07:36:17

问题


I have a pandas data frame df like:

Name  Hour Activity
    A   4   TT
    A   3   TT
    A   5   UU
    B   1   TT
    C   1   TT
    D   1   TT
    D   2   TT
    D   3   UU
    D   4   UU

The next step is to get the summation if the rows have identical value of the column Name and Activity.

For example, for the case Name: A and Activity: TT will give the summation of 7

The result is the presented as below

    TT  UU
A   7   5
B   1   0
C   1   0
D   3   7

Is it possible to do something like this using pandas groupby?


回答1:


Try groupby.sum and unstack

df_final = df.groupby(['Name', 'Activity']).Hour.sum().unstack(fill_value=0)

Out[177]:
Activity  TT  UU
Name
A          7   5
B          1   0
C          1   0
D          3   7



回答2:


A pivot_table would also work here:

df = pd.pivot_table(df, index='Name', columns='Activity', values='Hour', aggfunc='sum', fill_value=0)
df
Out[1]: 
Activity  TT  UU
Name            
A          7   5
B          1   0
C          1   0
D          3   7

From there, you could do some further clean up to get to:

df.columns = [''.join(col) for col in df.columns]
df = df.reset_index()
df
Out[2]: 
  Name  TT  UU
0    A   7   5
1    B   1   0
2    C   1   0
3    D   3   7


来源:https://stackoverflow.com/questions/64273360/how-to-groupby-two-columns-and-calculate-the-summation-of-rows-using-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!