Python 更改数据帧中的行顺序
我在Python 更改数据帧中的行顺序,python,pandas,sorting,jupyter-notebook,Python,Pandas,Sorting,Jupyter Notebook,我在python3的pandas中有以下数据框: +------------------------------+ | total_sum | percent_of_total | +--------------+------------------------------+ | Group | | | +--------------+----------------
python3
的pandas
中有以下数据框:
+------------------------------+
| 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','bastom+')
,但我得到了以下错误<代码>类型错误:不可损坏的类型:“列表”。看起来熊猫会按字母顺序对这些索引进行排序。如何对行重新排序?根据注释:
尝试使用
.reindex
:
.reindex(['11-', 'Just 12', 'Some College', 'Bachelor+'])
文档中有更多详细信息。您是否尝试过使用
.reindex(['11-'、'Just 12'、'Some College'、'badom+')
。医生有更多的细节。你就是那个男人!!这确实返回了我所需要的。@SSC如果您能将您的评论复制到答案部分,那就太好了