首页 > 解决方案 > 熊猫 groupby 并添加组的总和

问题描述


 A      B     C       D
1     foo    12    California
2     foo    22    California
3     bar    8     Rhode Island
4     bar    32    Rhode Island

Required output
 A      B      C    D
1     foo    12    California
2            22    California
foo Total         34
3     bar    8     Rhode Island
4            32    Rhode Island
bar Total        40

想要在每组的末尾添加总共的数字列

标签: pythonpandasnumpygroup-bypivot

解决方案


这里有两种方法。

使用groupy+ apply

(df.set_index('A')
   .groupby('B', as_index=False, sort=False)
   .apply(lambda d: pd.concat([d, d['C'].agg({'Total': 'sum'}).to_frame()]))
   .droplevel(0).rename_axis('A')  
   .reset_index()
   .fillna('')
)

输出:

       A    B   C             D
0      1  foo  12    California
1      2  foo  22    California
2  Total       34              
3      3  bar   8  Rhode Island
4      4  bar  32  Rhode Island
5  Total       40              

使用concatgroupby+ agg

(pd.concat([df,
            df.groupby('B', as_index=False).agg({'A': lambda x: 'Total', 'C': 'sum'}) 
           ])
   .sort_values(by=['B', 'A'])
   .assign(B=lambda d: d['B'].mask(d['A'].eq('Total')))
   .fillna('')
)

输出:

       A    B   C             D
2      3  bar   8  Rhode Island
3      4  bar  32  Rhode Island
0  Total       40              
0      1  foo  12    California
1      2  foo  22    California
1  Total       34              

推荐阅读