【发布时间】:2017-08-04 05:30:14
【问题描述】:
我在pandas 在python3 中有以下数据框:
+------------------------------+
| total_sum | percent_of_total |
+--------------+------------------------------+
| Group | | |
+--------------+------------------------------+
| 11- | 99435 | 0.255880 |
+--------------+-----------+------------------+
| Bachelor+ | 64900 | 0.167010 |
+--------------+-----------+------------------+
| Just 12 | 162483 | 0.418124 |
+--------------+-----------+------------------+
| Some College | 61782 | 0.158986 |
+---------------------------------------------+
我想改变行的顺序,包括像这样的索引......
+------------------------------+
| total_sum | percent_of_total |
+--------------+------------------------------+
| Group | | |
+--------------+------------------------------+
| 11- | 99435 | 0.255880 |
+--------------+-----------+------------------+
| Just 12 | 162483 | 0.418124 |
+--------------+-----------+------------------+
| Some College | 61782 | 0.158986 |
+--------------+-----------+------------------+
| Bachelor+ | 64900 | 0.167010 |
+--------------+-----------+------------------+
我尝试使用.sort_index(['11-', 'Just 12', 'Some College', 'Bachelor+']),但出现以下错误...TypeError: unhashable type: 'list'。所以看起来熊猫按字母顺序对这些索引进行排序。如何重新排序行?
【问题讨论】:
-
您是否尝试过使用
.reindex(['11-', 'Just 12', 'Some College', 'Bachelor+'])。reindex文档有更多详细信息。 -
你就是男人!那确实返回了我需要的东西。
-
@SSC 如果您将评论复制到答案部分会很棒
标签: python pandas sorting jupyter-notebook