这是我的目标输出,我想显示groupby sum() by pandas
但是我只能打印sum()或者detail
如何组合这两个输出
图片1是我的代码,图片2是我期望什么。
谢谢每一个人。
输入图片描述
输入图片描述
首先执行group by on 'area',然后使用function对该组执行sum()
def get_total(x):
x.loc['total'] = ['','',x['amount'].sum()]
return x
df.groupby(['area']).apply(get_total)