简体   繁体   中英

visualization with pandas in python

在此处输入图像描述 I have the following problem

I want to plot following table:

在此处输入图像描述

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

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:

在此处输入图像描述

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:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM