排序 pandas 系列 [英] Sorting a pandas series
本文介绍了排序 pandas 系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试找出如何以灵巧的方式对由于groupby聚合而生成的系列进行排序.
I am trying to figure out how to sort the Series generated as a result of a groupby aggregation in a smart way.
我像这样生成DataFrame的聚合:
I generate an aggregation of my DataFrame like this:
means = df.testColumn.groupby(df.testCategory).mean()
这将产生一个系列.我现在尝试按值对它进行排序,但会收到错误消息:
This results in a Series. I now try to sort this by value, but get an error:
means.sort()
...
-> Exception: This Series is a view of some other array, to sort in-place you must create a copy
然后我尝试创建一个副本:
I then try creating a copy:
meansCopy = Series(means)
meansCopy.sort()
-> Exception: This Series is a view of some other array, to sort in-place you must create a copy
如何使这种工作正常?
推荐答案
使用sort_values
,即means = means.sort_values()
. [ Pandas v0.17 + ]
Use sort_values
, i.e. means = means.sort_values()
. [Pandas v0.17+]
以前使用order()
方法的熊猫:means = means.order()
.
pandas used to use order()
method: means = means.order()
.
这篇关于排序 pandas 系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文