aceminer
aceminer

Reputation: 4295

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:

ValueError: Index contains duplicate entries, cannot reshape

Interest level has multiple duplicate values. What i am envisioning is that the lat/long would form a 2D diagram, with the interest level being the value in the map. Interest level is categorical with only 3 unique values.

Am i using the wrong class as i am following the seaborn heatmap tutorial http://seaborn.pydata.org/generated/seaborn.heatmap.html

Upvotes: 0

Views: 2358

Answers (1)

jezrael
jezrael

Reputation: 863611

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)

Upvotes: 2

Related Questions