2

Can someone please guide how to sort list of months?

INPUT = ['August', 'September', 'October', 'November', 'December', 'January']

OUTPUT= ['January','August', 'September', 'October', 'November', 'December']
ggorlen
  • 44,755
  • 7
  • 76
  • 106
manish
  • 55
  • 1
  • 8
  • 1
    You can use the approach in the second answer here [Sort a list of dates by months](https://stackoverflow.com/questions/32290801/sort-a-list-of-dates-by-month-in-python) – MT756 Mar 10 '20 at 19:26
  • @MT756, actually the accepted answer in the link you shared is the best approach. They just need different format code. – buran Mar 10 '20 at 19:30
  • @buran, right you could use %B as the format – MT756 Mar 10 '20 at 19:44

3 Answers3

9

calendar.month_name is an object that can be converted to a list of months in the correct sequence. You can sort by the index of each string in this list, assuming your input strings are well-formed:

>>> from calendar import month_name
>>> month_lookup = list(month_name)
>>> months = ['August', 'September', 'October', 'November', 'December', 'January']
>>> sorted(months, key=month_lookup.index)
['January', 'August', 'September', 'October', 'November', 'December']

You can also use datetime.strptime and the "%B" format string which matches the full month name.

>>> from datetime import datetime
>>> months = ['August', 'September', 'October', 'November', 'December', 'January']
>>> sorted(months, key=lambda m: datetime.strptime(m, "%B"))
['January', 'August', 'September', 'October', 'November', 'December']

Note that both solutions raise a ValueError on lists containing invalid month strings. The datetime version gives a clearer message.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
1

You can have the sort of months using this code:

import datetime
months_dict = {}
for i in range(1,13):
    months_dict[datetime.date(2020, i, 1).strftime('%B').lower()] = i
print(months_dict)
# {'january': 0, 'february': 1, 'march': 2, 'april': 3, 'may': 4, 'june': 5, 'july': 6, 'august': 7, 'september': 8, 'october': 9, 'november': 10, 'december': 11}

And then you can sort your list using sorted function:

months = ['August', 'September', 'October', 'November', 'December', 'January']
print(sorted(months, key=lambda x: months_dict[x.lower()]))
# ['January', 'August', 'September', 'October', 'November', 'December']
Alireza HI
  • 1,873
  • 1
  • 12
  • 20
0

Before:

Month Unit Cost Unit Price Cost Revenue
0 April 349.5117 404.5586 578.0353 670.268
1 August 312.0705 328.6801 513.1332 541.747
2 December 363.7626 376.0639 600.0729 619.8292
3 February 363.1559 416.4736 600.8566 690.7631
4 January 353.7365 411.386 576.6068 671.0083
5 July 324.0404 358.1196 522.9842 578.1788
6 June 384.2446 441.8092 634.0019 728.8003
7 March 354.2986 411.4245 584.3813 676.8101
8 May 380.1465 438.6138 619.3439 714.3145
9 November 339.0641 351.9415 556.4498 578.3473
from calendar import month_name
month_lookup = list(month_name)
data_group.Month = sorted(data_group.Month, key=month_lookup.index)

After:

Month Unit Cost Unit Price Cost Revenue
0 January 349.5117 404.5586 578.0353 670.268
1 February 312.0705 328.6801 513.1332 541.747
2 March 363.7626 376.0639 600.0729 619.8292
3 April 363.1559 416.4736 600.8566 690.7631
4 May 353.7365 411.386 576.6068 671.0083
5 June 324.0404 358.1196 522.9842 578.1788
6 July 384.2446 441.8092 634.0019 728.8003
7 August 354.2986 411.4245 584.3813 676.8101
8 September 380.1465 438.6138 619.3439 714.3145
9 October 339.0641 351.9415 556.4498 578.3473
moken
  • 3,227
  • 8
  • 13
  • 23