更改行顺序pandas数据框

时间:2017-03-14 01:56:56

标签: python pandas sorting jupyter-notebook

我在pandas的{​​{1}}中有以下数据框:

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

1 个答案:

答案 0 :(得分:0)

在评论中每个SSC


尝试使用.reindex

.reindex(['11-', 'Just 12', 'Some College', 'Bachelor+'])

reindex文档有更多详细信息。