Indika Rajapaksha
Indika Rajapaksha

Reputation: 1168

Pandas groupby sum and sort descending on that sum

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

enter image description here

Upvotes: 0

Views: 900

Answers (2)

user16836078
user16836078

Reputation:

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))

Upvotes: 1

Divyank
Divyank

Reputation: 1057

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.

Upvotes: 0

Related Questions