I have a DataFrame where one column contains month names (like “January”, “February”, etc.), but they’re not in calendar order. To fix this, I added a new column that maps each month name to its corresponding number (1–12).
Now I want to sort the DataFrame by that numeric column so the months appear in proper calendar sequence. What’s the correct way to do this using pandas sort by column?
Here’s a simplified version of the DataFrame:
0 1 2
0 354.7 April 4.0
1 55.4 August 8.0
...
How do I sort it by column 2 so the DataFrame reflects the correct month order?
Hey, I’ve done this kind of thing when visualizing monthly data in dashboards. If you already have a column (say, column 2) with the month numbers, you can sort the DataFrame
using pandas sort by column like this:
df = df.sort_values(by=2)
That’s it, Pandas will sort the rows based on the numeric month column in ascending order. If your month column is named instead of indexed, just replace 2 with the column name.
Just to add up, Here’s a cleaner way I’ve used when I don’t want to rely on a separate numeric column.
Pandas lets you define custom ordering using CategoricalDtype
, which works great for months:
import pandas as pd
months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"]
df[1] = pd.Categorical(df[1], categories=months, ordered=True)
df = df.sort_values(by=1) # pandas sort by column name
This way, you don’t need to maintain the numeric column separately. It’s great if your data changes frequently or you’re reading it from CSVs.
This is how I approached it when cleaning messy data in a CSV export. I first mapped month names to numbers and then used pandas sort by column to order them:
month_map = {
"January": 1, "February": 2, "March": 3, "April": 4,
"May": 5, "June": 6, "July": 7, "August": 8,
"September": 9, "October": 10, "November": 11, "December": 12
}
df[‘month_num’] = df[1].map(month_map)
df = df.sort_values(by=‘month_num’)
Once sorted, you can even drop the `month_num` column if you don’t need it. This method gives you full control and makes it obvious how sorting happens behind the scenes.