Using Seaborn Heatmap
I am running into some issues using heatmap in seaborn Code: df[['latitude', 'longitude', 'interest_level']] a = df.pivot('latitude', 'longitude', 'interest_level') Error: ValueEr
Solution 1:
You need pivot_table
with some aggregate function like mean
, sum
, ...:
#subset for pivot_table or groupby solution is not necessary, you can omit it#df = df[['latitude', 'longitude', 'interest_level']]a = df.pivot_table(index='latitude',
columns='longitude',
values='interest_level',
aggfunc='mean')
Or groupby
, aggregate function and unstack
:
a = df.groupby(['latitude','longitude'])['interest_level'].mean().unstack()
Sample:
df = pd.DataFrame({'latitude':[53,54,55,55],
'longitude':[10,11,12,12],
'interest_level':[1,5,2,6],
'another_col':[4,7,4,2]})
print (df)
another_col interest_level latitude longitude
0 4 1 53 10
1 7 5 54 11
2 4 2 55 12 <-duplicates for 55,12
3 2 6 55 12 <-duplicates for 55,12
a = df.pivot_table(index='latitude',
columns='longitude',
values='interest_level',
aggfunc='mean')
print (a)
longitude 10 11 12
latitude
53 1.0 NaN NaN
54 NaN 5.0 NaN
55 NaN NaN 4.0 <- (2+6)/2 = 4
Last:
ax = sns.heatmap(a)
Post a Comment for "Using Seaborn Heatmap"