简体   繁体   English

在 python 中使用 pandas 进行可视化

[英]visualization with pandas in python

在此处输入图像描述 I have the following problem我有以下问题

I want to plot following table:我想 plot 下表:

在此处输入图像描述

I want to compare the new_cases from germany and france per week how can i visualise this?我想每周比较来自德国和法国的 new_cases,我该如何可视化? 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:这里是 go:

sample df:样本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: Output:

在此处输入图像描述

I think you're trying to get a timeseries plot.我认为您正在尝试获取时间序列 plot。 For that you'll need to convert year_week to a datetime object.为此,您需要year_week转换为日期时间 object。 Subsequently you can groupby the country, and unstack and plot the timeseries:随后您可以按国家分组,并取消堆叠和groupby时间序列:

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:结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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