更改行顺序熊猫数据框

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'。所以看起来熊猫按字母顺序对这些索引进行排序。如何重新排序行?

div*_*san 7

根据评论中的SSC


尝试使用.reindex

.reindex(['11-', 'Just 12', 'Some College', 'Bachelor+'])
Run Code Online (Sandbox Code Playgroud)

reindex文档有更多详细信息。