Data Analysis with Python Projects - Page View Time Series Visualizer - Stuck on Month Sorting

Tell us what’s happening:
Like many others taking this course, my code is failing because the months in the DataFrame are sorted alphabetically.

I can’t find any solution to this problem that wouldn’t involve just deleting all my code and copying someone else’s, so I want you to review my code and help me sort the months in my dataframe.

Your code so far

# Copy and modify data for monthly bar plot
df_bar = df.copy()
df_bar.reset_index(inplace=True)

df_bar['year'] = [d.year for d in df_bar.date]
df_bar['month'] = [d.stftime('%b') for d in df_bar.date]

# Create monthly average plot
df_avg_bar = pd.DataFrame(df_bar.groupby(['year', 'month'])['value'].mean().round(decimals = 2))
df_avg_bar.reset_index(inplace = True)

# Draw bar plot
fig, ax = plt.subplots(figsize=(10, 15))
sns.barplot(data = df_avg_bar, x = 'year', y = 'value', hue = 'month')
plt.xlabel('Years')
plt.ylabel('Average Page Views')
plt.legend(loc='upper left')

I’ve also considered avoiding changing the month to a string until after creating the new dataframe by replacing d.stftime('%b') with d.month and then coming up with a way to remap the month names to the column, but it seems like Pandas doesn’t want you to modify the values in the dataframe.

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/118.0

Challenge: Data Analysis with Python Projects - Page View Time Series Visualizer

Link to the challenge:

1 Like

This is the way.

My post must be at least 20 characters, this is filler.