Pandas:排序数据透视表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10595327/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 15:43:20  来源:igfitidea点击:

Pandas: Sort pivot table

pythonpandas

提问by mrmagooey

Just trying out pandas for the first time, and I am trying to sort a pivot table first by an index, then by the values in a series.

只是第一次尝试熊猫,我试图首先按索引对数据透视表进行排序,然后按系列中的值排序。

So far I've tried:

到目前为止,我已经尝试过:

table = pivot_table(sheet1, values='Value', rows=['A','B'], aggfunc=np.sum)

# Sorts by value ascending, can't change to descending
table.copy().sort()
table

# The following gives me the correct ordering in values, but ignores index 
sorted_table = table.order(ascending=False)
sorted_table

# The following brings me back to the original ordering
sorted_table = table.order(ascending=False)
sorted_table2 = sorted_table.sortlevel(0)
sorted_table2

What's the correct way to sort a pivot table by index then value?

按索引然后按值对数据透视表进行排序的正确方法是什么?

回答by Wes McKinney

Here is a solution that may do what you want:

这是一个可以满足您要求的解决方案:

key1 = table.index.labels[0]
key2 = table.rank(ascending=False)

# sort by key1, then key2
sorter = np.lexsort((key2, key1))

sorted_table = table.take(sorter)

The result would look like this:

结果如下所示:

In [22]: table
Out[22]: 
A    B    
bar  one      0.698202
     three    0.801326
     two     -0.205257
foo  one     -0.963747
     three    0.120621
     two      0.189623
Name: C

In [23]: table.take(sorter)
Out[23]: 
A    B    
bar  three    0.801326
     one      0.698202
     two     -0.205257
foo  two      0.189623
     three    0.120621
     one     -0.963747
Name: C

This would be good to build into pandas as an API method. Not sure what it should look like though.

将其作为 API 方法构建到 Pandas 中会很好。不知道它应该是什么样子。