Valador1988
Valador1988

Reputation: 51

How to convert Multi-Index into a Heatmap

New to Pandas/Python, I have managed to make an index like below;

MultiIndex([( 1,  1,  4324),
            ( 1,  2,  8000),
            ( 1,  3,  8545),
            ( 1,  4, 8544),
            ( 1,  5, 7542),
            (12, 30, 7854),
            (12, 31, 7511)],
            names=['month', 'day', 'count'], length=366)

I'm struggling to find out how I can store the first number into a list (the 1-12 one) the second number into another list (1-31 values) and the third number into another seperate list (scores 0-9000)

I am trying to build a heatmap that is Month x Day on the axis' and using count as the values and failing horribly! I am assuming I have to seperate Month, Day and Count into seperate lists to make the heat map?

data1 = pd.read_csv("a2data/Data1.csv")
data2 = pd.read_csv("a2data/Data2.csv")
merged_df = pd.concat([data1, data2])
merged_df.set_index(['month', 'day'], inplace=True)
merged_df.sort_index(inplace=True)
merged_df2=merged_df.groupby(['month', 'day']).count.mean().reset_index()
merged_df2.set_index(['month', 'day', 'count'], inplace=True)

#struggling here to seperate out month, day and count in order to make a heatmap

Upvotes: 1

Views: 1341

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

Are you looking for:

# let start here
merged_df2=merged_df.groupby(['month', 'day']).count.mean()

# use sns
import seaborn as sns
sns.heatmap(merged_df2.unstack('day'))

Output:

enter image description here

Or you can use plt:

merged_df2=merged_df.groupby(['month', 'day']).count.mean().unstack('day')

plt.imshow(merged_df2)
plt.xticks(np.arange(merged_df2.shape[1]), merged_df2.columns)
plt.yticks(np.arange(merged_df2.shape[0]), merged_df2.index)
plt.show()

which gives:

enter image description here

Upvotes: 2

Related Questions