Home > Mobile >  python pandas groupby detail and sum [closed]
python pandas groupby detail and sum [closed]

Time:10-02

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.

enter image description here

enter image description here

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)

  • Related