根据月份重新排序系列数据

问题描述 投票:0回答:1

我有一个包含以下数据的数据框:

month
April        4484.900000
August       5664.419355
December     3403.806452
February     2655.298246
January      2176.338710
July         5563.677419
June         5772.366667
March        3692.258065
May          5349.774194
November     4247.183333
October      5199.225806
September    5766.516667

我希望数据按一月到十二月的顺序排列,而不是按字母顺序排列。有没有办法重新排序?

python python-3.x dataframe series
1个回答
0
投票

您可以根据

reindex
month_name
:

from calendar import month_name

out = s.reindex(month_name[1:]) # with `s` your Series

输出:

print(out)

month
January      2176.338710
February     2655.298246
March        3692.258065
April        4484.900000
May          5349.774194
June         5772.366667
July         5563.677419
August       5664.419355
September    5766.516667
October      5199.225806
November     4247.183333
December     3403.806452
dtype: float64
© www.soinside.com 2019 - 2024. All rights reserved.