Gil*_*ert 12 python sorting pandas jupyter-notebook
我在pandasin 中有以下数据框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 |
+---------------------------------------------+
Run Code Online (Sandbox Code Playgroud)
我想改变行的顺序,包括像这样的索引......
+------------------------------+
| total_sum | percent_of_total |
+--------------+------------------------------+
| Group | | |
+--------------+------------------------------+
| 11- | 99435 | 0.255880 |
+--------------+-----------+------------------+
| Just 12 | 162483 | 0.418124 |
+--------------+-----------+------------------+
| Some College | 61782 | 0.158986 |
+--------------+-----------+------------------+
| Bachelor+ | 64900 | 0.167010 |
+--------------+-----------+------------------+
Run Code Online (Sandbox Code Playgroud)
我试图用.sort_index(['11-', 'Just 12', 'Some College', 'Bachelor+'])我碰到下面的错误...... TypeError: unhashable type: 'list'。所以看起来熊猫按字母顺序对这些索引进行排序。如何重新排序行?
根据评论中的SSC:
尝试使用.reindex:
.reindex(['11-', 'Just 12', 'Some College', 'Bachelor+'])
Run Code Online (Sandbox Code Playgroud)
该reindex文档有更多详细信息。