Aman Rao
Aman Rao

Reputation: 65

How to sort pandas dataframe by month name

I have the following data frame:

https://i.sstatic.net/b5CMd.png (44 rows)

I tried sorting it by using CategoricalIndex() but found out it can only be done if there are no repeat in month values. Any one know how to sort it chronologically.

Upvotes: 1

Views: 1158

Answers (3)

marinovik
marinovik

Reputation: 606

Always try to post your code. In this way we could figure out why your categorical sorting did not work out. But I suspect you forgot the ordered=True parameter.

Categorical ordering allows sorting according to a custom order, and works perfectly for this case. It also handles well duplicated month values. Here is my code:

df["month"] = pd.Categorical(df["month"],
                             categories=["January", "February", "March", "April", "May", "June", "July",
                                         "August", "September", "October", "November", "December"],
                             ordered=True)

And after that we can call the function sort_values():

df = df.sort_values(["year", "month"], ignore_index=True)

Cheers and keep it up!

Upvotes: 1

Nev1111
Nev1111

Reputation: 1049

You can also try:

df['date']=(df['month']+' '+df['year'])
df['date']=pd.to_datetime(df['date'])
df=df.sort_values('date')

Upvotes: 0

wordinone
wordinone

Reputation: 131

Hope it helps

# Add new column in format YYYYMM
df['year_month'] = df['year'] + (pd.to_datetime(df.month , format='%B').dt.month.astype(str).str.zfill(2))
# sort values
df.sort_values(by=['year_month'], inplace=True)

Upvotes: 0

Related Questions