【问题标题】:Change rows order pandas data frame更改行顺序熊猫数据框
【发布时间】:2017-08-04 05:30:14
【问题描述】:

我在pandaspython3 中有以下数据框:

               +------------------------------+
               | 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


【解决方案1】:

每个 cmets 中的 SSC


尝试使用.reindex

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

reindex 文档有更多详细信息。

【讨论】:

    猜你喜欢
    • 2021-06-28
    • 2020-12-09
    • 1970-01-01
    • 2019-10-20
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 1970-01-01
    • 2016-05-16
    相关资源
    最近更新 更多