0

enter image description hereI have the following problem

I want to plot following table:

enter image description here

I want to compare the new_cases from germany and france per week how can i visualise this? I already tried multiple plots but I'm not happy with the results:

for example:

pivot_df['France'].plot(kind='bar')
plt.figure(figsize=(15,5))
pivot_df['France'].plot(kind='bar')
plt.figure(figsize=(15,5))`

But it only shows me france

Work_fl0w
  • 21
  • 4

2 Answers2

0

Here you go:

sample df:

   Country  week  new_cases
0   FRANCE     9        210
1  GERMANY     9        300
2   FRANCE    10        410
3  GERMANY    10        200
4   FRANCE    11        910
5  GERMANY     9        500

Code:

df.groupby(['week','Country'])['new_cases'].sum().unstack().plot.bar()
plt.ylabel('New cases')

Output:

enter image description here

sharathnatraj
  • 1,614
  • 5
  • 14
0

I think you're trying to get a timeseries plot. For that you'll need to convert year_week to a datetime object. Subsequently you can groupby the country, and unstack and plot the timeseries:

import datetime
import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('https://opendata.ecdc.europa.eu/covid19/testing/csv/data.csv')

df = df[df['country'].isin(['France', 'Germany'])]
df = df[df['level'] == 'national'].reset_index()

df['datetime'] = df['year_week'].apply(lambda x: datetime.datetime.strptime(x + '-1', '%G-W%V-%u')) #https://stackoverflow.com/a/54033252/11380795
df.set_index('datetime', inplace=True)

grouped_df = df.groupby('country').resample('1W').sum()['new_cases']
ax = grouped_df.unstack().T.plot(figsize=(10,5))
ax.ticklabel_format(style='plain', axis='y')  

result:

enter image description here

RJ Adriaansen
  • 9,131
  • 2
  • 12
  • 26