Mike
Mike

Reputation: 150

Merge pandas dataframe with dataframe from csv

I have a csv file where each row has a date yyyy-MM-dd and a count (e.g. 1, 2, 6).

2020-06-08,53
202-06-09,12

I read this into a dataframe as such and index on the date:

import pandas as pd

data_df = pd.read_csv('data.csv', header=0, names=['date', 'count'])
data_df['dt'] = pd.to_datetime(data_df['date'])
data_df = data_df.set_index('dt')
data_df.drop(['date'], axis=1, inplace=True)

I then create a dataframe for a date range:

date_rng = pd.date_range(start='1/1/2020', end='12/31/2020', freq='D')
df = pd.DataFrame(date_rng, columns=['date'])
df['dt'] = pd.to_datetime(df['date'])
df = df.set_index('dt')
df.drop(['date'], axis=1, inplace=True)
df['count'] = 0

Then I merged them:

df.update(data_df)

When I print df it looks correct - it contains the default data, and where present in the csv, contains the actual count from the corresponding csv row.

I then use the calmap module (https://pythonhosted.org/calmap/) to generate a heatmap over the year:

calmap.yearplot(df, year=2020, cmap='YlGn', daylabels='SMTWTFS', fig_kws=dict(figsize=(8, 4)))

I get this error:

ValueError: Shape of passed values is (1, 4), indices imply (366, 4)

I am missing something obvious. Most of the above code is lifted from somewhere.

Any help is appreciated. I am pretty new to pandas and I am working through some tutorials but so far nothing.

Upvotes: 0

Views: 73

Answers (1)

BENY
BENY

Reputation: 323246

Can you try pass Series

calmap.yearplot(df['count'], year=2020, cmap='YlGn', daylabels='SMTWTFS', fig_kws=dict(figsize=(8, 4)))

Upvotes: 1

Related Questions