pandas groupby sort descending order

前端 未结 6 1396
离开以前
离开以前 2020-12-28 13:02

pandas groupby will by default sort. But I\'d like to change the sort order. How can I do this?

I\'m guessing that I can\'t apply a sort method to the returned gro

6条回答
  •  生来不讨喜
    2020-12-28 13:20

    Do your groupby, and use reset_index() to make it back into a DataFrame. Then sort.

    grouped = df.groupby('mygroups').sum().reset_index()
    grouped.sort_values('mygroups', ascending=False)
    

提交回复
热议问题