Taiguara Cavaliere
Taiguara Cavaliere

Reputation: 47

Plot different occurrences per date

I have a dataset with many columns and two of them are:

Type of vaccine Date
A 01-01-2021
A 01-01-2021
B 01-01-2021
C 02-01-2021
B 02-01-2021
D 03-01-2021
E 04-01-2021
... ...

I want to plot a line graph showing every type of vaccine and it's amount per date.

I have a plot like this plot2 = df.value_counts('vacina_dataaplicacao').sort_values().plot(kind='line', cmap='plasma') but it only show one line and the totals per date. I need to separate it, one line for each type of vaccine.

There's the result of that graph I already have.

Upvotes: 1

Views: 662

Answers (1)

Pierre D
Pierre D

Reputation: 26221

You can do it in one line:

pd.crosstab(pd.to_datetime(df['Date']), df['Type of vaccine']).cumsum().plot()

Credit to @TrentonMcKinney for the idea of using pd.crosstab!

Initially, I was going with basic groupby:

df = df.assign(Date=pd.to_datetime(df['Date']))
tally = df.groupby(['Date', 'Type of vaccine']).size().unstack(fill_value=0)

tally.cumsum().plot()

Upvotes: 2

Related Questions