Reputation: 173
I have a Dataframe containing 'Date'
column and 'Tweet_Count'
column.
I want to plot a heatmap that will show tweet counts on each day of one month.
Using this code:
uk = uk_df.pivot("Date", "Tweet_Count")
ax = sns.heatmap(uk)
I am getting an error:
TypeError: '<=' not supported between instances of 'float' and 'str'
Any help would be really appreciated. Thank you.
Upvotes: 2
Views: 652
Reputation: 46908
Let's say the data looks like this, below I have two countries
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
Date = pd.date_range(start='1/3/2020', periods=30,freq="D").strftime('%Y-%m-%d').to_list()
df = pd.DataFrame({'Country':np.repeat(['UK','KU'],len(Date)),
'Date':Date*2,
'Tweet_Count':np.random.randint(1000,2000,len(Date)*2)})
df.head()
Country Date Tweet_Count
0 UK 2020-01-03 1809
1 UK 2020-01-04 1419
2 UK 2020-01-05 1463
3 UK 2020-01-06 1576
4 UK 2020-01-07 1137
If there's only 1 country, then a transpose will work:
fig, ax = plt.subplots(figsize=(8,2))
uk = df[df['Country']=="UK"]
ax = sns.heatmap(uk[['Date','Tweet_Count']].set_index('Date').T)
If there's 2 or more:
wide_df = df.pivot_table(index="Country",columns="Date",fill_value="Tweet_Count")
wide_df.columns = [j for i,j in wide_df.columns]
fig, ax = plt.subplots(figsize=(8,2))
ax = sns.heatmap(wide_df)
Upvotes: 1
Reputation: 16683
The required DataFrame format for a sns.heatmap
you are looking for is a pivot table with an index
, columns
and values
passed as Date
, Country
and Tweet_Count
to pivot
, respectively, below (if this causes an error, please upgrade to the latest version of pandas
as pivot
had issues in past versions):
Country UK
Date
2020-03-01 400
2020-03-02 1000
2020-03-03 100
So, you simply need to pass Country
to pivot as well:
uk = pd.DataFrame({'Country': {0: 'UK', 1: 'UK', 2: 'UK'},
'Date': {0: '2020-03-01', 1: '2020-03-02', 2: '2020-03-03'},
'Tweet_Count': {0: 100, 1: 200, 2: 300}})
uk['Date'] = pd.to_datetime(uk['Date']).dt.date
uk = uk.pivot("Date", "Country", 'Tweet_Count')
ax = sns.heatmap(uk)
Scroll down to the fourth block of code here:
It's worth mentioning that you can also annotate with:
ax = sns.heatmap(uk, annot=True, fmt="d")
Upvotes: 1