Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reorder Series Data Based on Month

I have a data frame with the following data:

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

I want the data to be ordered from January to December, not alphabetically. Is there a way to reorder this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can reindex it based on the month_name :

from calendar import month_name

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

Output :

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

Used input :

s = pd.read_clipboard().squeeze().rename(None).rename_axis("month")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading