Dec 47
Nov 36
Oct 14
Sep 2
Jan 2
Aug 2
May 1
Apr 1
Jun 1
Jul 1
Feb 1
Name: date, dtype: int64
I'm tring to sort the above series whose index column is month, by month. However instead of sorting by month's calendar order the sort function is sorting by dictionary order of the month name. How can I sort the above correctly? Guess I have to specify that the index type is month and not string. Any help is appreciated. Code snippet below.
import calendar
movies = release_dates[release_dates.title.str.contains('Christmas') & (release_dates.country=='USA')]
movies = movies.date.dt.month.apply(lambda x: calendar.month_abbr[x])
counts = movies.value_counts()
counts
You can use sorted CategoricalIndex
with sort_index
:
cats = ['Jan', 'Feb', 'Mar', 'Apr','May','Jun', 'Jul', 'Aug','Sep', 'Oct', 'Nov', 'Dec']
df.index = pd.CategoricalIndex(df.index, categories=cats, ordered=True)
df = df.sort_index()
print (df)
date
Jan 2
Feb 1
Apr 1
May 1
Jun 1
Jul 1
Aug 2
Sep 2
Oct 14
Nov 36
Dec 47
Or use DataFrame.reindex
- but if some value is missing add NaNs rows:
df = df.reindex(cats)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With