Home > Back-end >  Pandas groupby sum and sort descending on that sum
Pandas groupby sum and sort descending on that sum

Time:08-29

I have a groupby statement resulting the below result. How can sort the w column descending order within that grouping?

enter image description here

CodePudding user response:

Code -

grouped = df_sp.groupby(['season','home_team']).sum('w').reset_index()
g = grouped.sort_values('w', ascending=False)

Ref link - pandas groupby sort descending order

pls share reproducible code so that others can test, above code is not tested.

CodePudding user response:

You can use groupby.apply with the sort_values

df_sp.groupby(['season','home_team']).sum('w').apply(lambda x : x['w'].sort_values(ascending=False))
  • Related