如何在 Pandas Python 中从最大到最小的 groupby 数据排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40079627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to sort from greatest to smallest of groupby data in Pandas Python
提问by dancemc15
I would like to sort the number of occurrences that both the street name
+ cross
name appear together from largest to smallest.
我想将街道name
+cross
名称一起出现的次数从大到小排序。
dataset=df.groupby(['Street Name', 'Cross Street']).size()
How do I sort this list in a Pandas dataframe?
如何在 Pandas 数据框中对这个列表进行排序?
回答by ayhan
It returns a Series so you can use the sort_values
method of the Series:
它返回一个系列,以便您可以使用sort_values
系列的方法:
df.groupby(['Street Name', 'Cross Street']).size().sort_values()
Or, in descending order:
或者,按降序排列:
df.groupby(['Street Name', 'Cross Street']).size().sort_values(ascending=False)