将索引重新排序到特定顺序

Reordering Index to a specific order

我有一个名为 exam 的数据框,我想根据常见顺序(一月、二月、三月、四月等)对月份列(即索引)进行排序。但是,它似乎是按升序排序的。我尝试了 reindex 命令,但没有任何区别。怎样才能随心所欲地订购?

这是代码

exam.reindex(['January','February','March','April','May','June','July','August','September','October','November'])

这是数据框

试试这个:

new_index = ['January','February','March','April','May','June','July','August','September','October','November']
exam = exam.loc[new_index]

您需要将其分配回来

exam = exam.reindex(['January','February','March','April','May','June','July','August','September','October','November'])