Sort column in Pandas DataFrame by specific order

  • A+
Category:Languages

Given this DataFrame:

df = pd.DataFrame([['August', 2], ['July', 3], ['Sept', 6]], columns=['A', 'B']) 

I would like to sort column A in this order: July, August, Sept. Is there some way to use a sort function like "sort_values" but pre-define the sort order by values?

 


Are you opposed to using either complete month names or consistent abbreviations?

df = pd.DataFrame([['August', 2], ['July', 3], ['Sept', 6]], columns=['A', 'B'])  df  import calendar  df = df.replace({'Sept':'September'})  calendar.month_name[1:] 

Output:

['January',  'February',  'March',  'April',  'May',  'June',  'July',  'August',  'September',  'October',  'November',  'December']  df['A'] = pd.Categorical(df.A, categories=calendar.month_name[1:], ordered=True)  df.sort_values('A') 

Output:

           A  B 1       July  3 0     August  2 2  September  6 

Or use calendar.month_abbr

calendar.month_abbr[1:] 

Output:

['Jan',  'Feb',  'Mar',  'Apr',  'May',  'Jun',  'Jul',  'Aug',  'Sep',  'Oct',  'Nov',  'Dec'] 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: