排序熊猫系列

vas*_*ek1 22 python sorting series pandas

我试图弄清楚如何以智能方式对作为groupby聚合的结果生成的Series进行排序.

我生成一个我的DataFrame的聚合,如下所示:

means = df.testColumn.groupby(df.testCategory).mean()
Run Code Online (Sandbox Code Playgroud)

这导致了一个系列.我现在尝试按值排序,但得到一个错误:

means.sort()
...
-> Exception: This Series is a view of some other array, to sort in-place you must create a copy
Run Code Online (Sandbox Code Playgroud)

然后我尝试创建一个副本:

meansCopy = Series(means)
meansCopy.sort()
-> Exception: This Series is a view of some other array, to sort in-place you must create a copy
Run Code Online (Sandbox Code Playgroud)

我怎样才能使用这种方法?

Wes*_*ney 30

熊猫v0.17 +

使用sort_values,即means = means.sort_values().

原始答案,前v0.17

尝试使用order(),即means = means.order().

  • 所有这些.sort_index(),. argsort()等都非常令人困惑...... doc并没有多说...... (5认同)