Python Pandas 中的多索引排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14733871/
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
Multi Index Sorting in Pandas
提问by MattB
I have a dataset with multi-index columns in a pandas df that I would like to sort by values in a specific column. I have tried using sortindex and sortlevel but haven't been able get the results I am looking for. My dataset looks like:
我在 Pandas df 中有一个包含多索引列的数据集,我想按特定列中的值排序。我曾尝试使用 sortindex 和 sortlevel,但无法获得我正在寻找的结果。我的数据集看起来像:
Group1 Group2
A B C A B C
1 1 0 3 2 5 7
2 5 6 9 1 0 0
3 7 0 2 0 3 5
I want to sort all data and the index by column C in Group 1 in descending order so my results look like:
我想按第 1 组中的 C 列按降序对所有数据和索引进行排序,因此我的结果如下所示:
Group1 Group2
A B C A B C
2 5 6 9 1 0 0
1 1 0 3 2 5 7
3 7 0 2 0 3 5
Is it possible to do this sort with the structure that my data is in, or should I be swapping Group1 to the index side?
是否可以使用我的数据所在的结构进行这种排序,还是应该将 Group1 交换到索引端?
采纳答案by Andy Hayden
When sorting by a MultiIndex you need to contain the tuple describing the column inside a list*:
当按 MultiIndex 排序时,您需要包含描述列表中列的元组*:
In [11]: df.sort_values([('Group1', 'C')], ascending=False)
Out[11]:
Group1 Group2
A B C A B C
2 5 6 9 1 0 0
1 1 0 3 2 5 7
3 7 0 2 0 3 5
* so as not to confuse pandas into thinking you want to sort first by Group1 then by C.
*以免让大熊猫误以为您想先按 Group1 然后按 C 排序。
Note: Originally used .sortsince deprecated then removed in 0.20, in favor of .sort_values.
注意:最初使用.sort自弃用,然后在 0.20 中删除,支持.sort_values.

