Dinosaurius Dinosaurius - 25 days ago 7
Python Question

Sorting does not work for my heatmap

I have the following DataFrame

df_imd
(a small extract is given):

id weekday daily_avg_count
A13 7 37.000000
A13 1 46.470588
A13 6 46.812500
B14 7 46.875000
C15 4 48.294118


I cannot figure out why weekdays do not get sorted in the heatmap after running the following code:

norm = plt.Normalize(df_imd["daily_avg_count"].values.min(), df_imd["daily_avg_count"].values.max())
colors = plt.cm.YlGnBu(norm(df_imd["daily_avg_count"]))

df_imd.loc[df_imd['weekday'] == "1", 'weekday'] = "Monday"
df_imd.loc[df_imd['weekday'] == "2", 'weekday'] = "Tuesday"
df_imd.loc[df_imd['weekday'] == "3", 'weekday'] = "Wednesday"
df_imd.loc[df_imd['weekday'] == "4", 'weekday'] = "Thursday"
df_imd.loc[df_imd['weekday'] == "5", 'weekday'] = "Friday"
df_imd.loc[df_imd['weekday'] == "6", 'weekday'] = "Saturday"
df_imd.loc[df_imd['weekday'] == "7", 'weekday'] = "Sunday"

sorter = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
df_imd.weekday = df_imd.weekday.astype("category")
df_imd.weekday.cat.set_categories(sorter, inplace=True)

iterables = [df_imd['id'].unique(),df_imd['weekday'].unique()]
df = df_imd.set_index(['id','weekday'])
df = df.reindex(index=pd.MultiIndex.from_product(iterables, names=['id', 'weekday']), fill_value=0).reset_index()

df = df.pivot("weekday", "id", "daily_avg_count")

plt.figure(figsize=(12,8))
ax = sns.heatmap(df, annot=False, linewidths=.5, cmap="YlGnBu")
plt.show()


Instead of getting "Monday", "Tuesday", etc., I see "Wednesday", "Monday", etc.

Answer Source

You need change:

df_imd.weekday = df_imd.weekday.astype("category")
df_imd.weekday.cat.set_categories(sorter, inplace=True)

to:

df_imd.weekday = df_imd.weekday.astype("category", ordered=True, categories=sorter)

for ordered categorical.