here is my target output, I want to show groupby sum() by pandas
But I can only print sum() or detail
How can I combine this two output
pic 1 is my code , pic 2 is what I expect.
Thanks everyone.
CodePudding user response:
First do group by on 'area' and then do sum() on that group using lambda function
def get_total(x):
x.loc['total'] = ['','',x['amount'].sum()]
return x
df.groupby(['area']).apply(get_total)