本文介绍了 pandas groupby排序降序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
pandas groupby将默认排序.但我想更改排序顺序.我该怎么办?
pandas groupby will by default sort. But I'd like to change the sort order. How can I do this?
我猜想我不能对返回的groupby对象应用排序方法.
I'm guessing that I can't apply a sort method to the returned groupby object.
推荐答案
执行groupby,并使用reset_index()将其放回到DataFrame中.然后排序.
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)
这篇关于 pandas groupby排序降序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!