[英]Pandas: Sort pivot table
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? 按索引排序数据透视表的正确方法是什么?
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. 这可以作为API方法构建到pandas中。 Not sure what it should look like though.
不知道它应该是什么样子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.