目录
2.中国大陆、印度、美国和其他国家或地区确诊人数随时间走势图
2.中国大陆、印度、美国和其他国家或地区死亡人数随时间走势图
# 导入相关的包
import numpy as np
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
import plotly.io as pio
pio.templates.default = 'plotly_dark'
from plotly.subplots import make_subplots
一、读取数据
# parse_dates将对象类型转化为datetime类型
df = pd.read_csv('../../covid19-global-forecasting-week-1/covid_19_data.csv',parse_dates=['ObservationDate','Last Update'])
df.head()
二、数据预处理
# 列名重新命名
df.rename(columns={'ObservationDate': 'date',
'Province/State':'state',
'Country/Region':'country',
'Last Update':'last_updated',
'Confirmed': 'confirmed',
'Deaths':'deaths',
'Recovered':'recovered'
}, inplace=True)
df.head()
# cases
cases = ['confirmed', 'deaths', 'recovered', 'active']
# 增加新的一列active
df['active'] = df['confirmed'] - df['deaths'] - df['recovered']
# 填充缺失值,
df['state'] = df['state'].fillna('')
df[cases] = df[cases].fillna(0)
data = df
# 数据的起始时间和总天数
print("External Data")
print(f"Earliest Entry: {df['date'].min()}")
print(f"Last Entry: {df['date'].max()}")
print(f"Total Days: {df['date'].max() - df['date'].min()}")
三、确诊病例分析
1.全球确诊病例随时间变化
grouped = data.groupby('date')[['date', 'confirmed', 'deaths']].sum().reset_index()
fig = px.line(grouped, x="date", y="confirmed",
title='全球确诊病例随时间增长趋势图')
fig.show()
fig = px.line(grouped, x="date", y="confirmed",
title='全球确诊病例随时间增长对数图',
log_y=True)
fig.show()
2.中国大陆、印度、美国和其他国家或地区确诊人数随时间走势图
# 中国大陆
grouped_china = data[data['country'] == "Mainland China"].reset_index()
grouped_china_date = grouped_china.groupby('date')[['date', 'confirmed', 'deaths']].sum().reset_index()
# 印度
grouped_india = data[data['country'] == "India"].reset_index()
grouped_india_date = grouped_india.groupby('date')[['date', 'confirmed', 'deaths']].sum().reset_index()
# 美国
grouped_us = data[data['country'] == "US"].reset_index()
grouped_us_date = grouped_us.groupby('date')[['date', 'confirmed', 'deaths']].sum().reset_index()
# 其他国家
grouped_rest = data[~data['country'].isin(['China', 'Italy', 'US'])].reset_index()
grouped_rest_date = grouped_rest.groupby('date')[['date', 'confirmed', 'deaths']].sum().reset_index()
plot_titles = ['Mainland China', 'India', 'USA', 'Rest of the World']
fig = px.line(grouped_china_date, x="date", y="confirmed",
title=f"Confirmed Cases in {plot_titles[0].upper()} Over Time",
color_discrete_sequence=['#F61067'],
height=500
)
fig.show()
fig = px.line(grouped_india_date, x="date", y="confirmed",
title=f"Confirmed Cases in {plot_titles[1].upper()} Over Time",
color_discrete_sequence=['#91C4F2'],
height=500
)
fig.show()
fig = px.line(grouped_us_date, x="date", y="confirmed",
title=f"Confirmed Cases in {plot_titles[2].upper()} Over Time",
color_discrete_sequence=['#6F2DBD'],
height=500
)
fig.show()
fig = px.line(grouped_rest_date, x="date", y="confirmed",
title=f"Confirmed Cases in {plot_titles[3].upper()} Over Time",
color_discrete_sequence=['#FFDF64'],
height=500
)
fig.show()
3.全球确诊人数分布图
# 最新确诊情况,使用最后一天的数据来分析
temp = data[[col for col in data.columns if col != 'state']]
latest = temp[temp['date'] == max(temp['date'])].reset_index()
latest_grouped = latest.groupby('country')[['confirmed','deaths']].sum().reset_index()
latest_grouped.head(10)
确诊人数的地理分布情况
# 确诊人数的地理分布情况
fig = px.choropleth(latest_grouped, locations="country",
locationmode='country names', color="confirmed",
hover_name="country", range_color=[1,500000],
color_continuous_scale="peach",
title='Countries with Confirmed Cases')
# fig.update(layout_coloraxis_showscale=False)
fig.show()
美国、俄罗斯、印度和欧洲大部分的国家是重灾区
4.确诊人数排名前十位的国家
fig = px.bar(latest_grouped.sort_values('confirmed', ascending=False)[:10][::-1],
x='confirmed', y='country',
title='Confirmed Cases Worldwide', text='confirmed', height=1000, orientation='h')
fig.show()
美国、印度、巴西的确诊人数最多
5.美国确诊人数前十的州
usa = data[data['country'] == "US"]
usa_latest = usa[usa['date'] == max(usa['date'])]
usa_latest = usa_latest.groupby('state')[['confirmed', 'deaths']].max().reset_index()
fig = px.bar(usa_latest.sort_values('confirmed', ascending=False)[:10][::-1],
x='confirmed', y='state', color_discrete_sequence=['#D63230'],
title='Confirmed Cases in USA', text='confirmed', orientation='h')
fig.show()
美国的加利福尼亚州的确诊人数最多,是纽约州的1.5倍。
6.中国大陆确诊人数排名前十的省市
china = data[data['country'] == "Mainland China"]
china_latest = china[china['date'] == max(china['date'])]
china_latest = china_latest.groupby('state').max()[['confirmed','deaths']].reset_index()
fig = px.bar(china_latest.sort_values('confirmed',ascending=False)[:10][::-1],
x='confirmed',y='state')
fig.show()
中国大陆的湖北省的确诊人数最多
四. 死亡人数分析
1. 全球死亡人数随时间增长情况
fig = px.line(grouped, x="date", y="deaths", title="Worldwide Deaths Over Time",
color_discrete_sequence=['#F42272'])
fig.show()
fig = px.line(grouped, x="date", y="deaths", title="Worldwide Deaths (Logarithmic Scale) Over Time",
log_y=True, color_discrete_sequence=['#F42272'])
fig.show()
自3月初以来,全球死亡人数开始急剧上升。
2.中国大陆、印度、美国和其他国家或地区死亡人数随时间走势图
plot_titles = ['Mainland China', 'India', 'USA', 'Rest of the World']
fig = px.line(grouped_china_date, x="date", y="deaths",
title=f"Deaths in {plot_titles[0].upper()} Over Time",
color_discrete_sequence=['#F61067'],
height=500,labels={}
)
fig.show()
fig = px.line(grouped_india_date, x="date", y="deaths",
title=f"Deaths in {plot_titles[1].upper()} Over Time",
color_discrete_sequence=['#91C4F2'],
height=500
)
fig.show()
fig = px.line(grouped_us_date, x="date", y="deaths",
title=f"Deaths in {plot_titles[2].upper()} Over Time",
color_discrete_sequence=['#6F2DBD'],
height=500
)
fig.show()
fig = px.line(grouped_rest_date, x="date", y="deaths",
title=f"Deaths in {plot_titles[3].upper()} Over Time",
color_discrete_sequence=['#FFDF64'],
height=500
)
fig.show()
1.中国大陆在3月初到5月初,死亡人数激增之外,5月之后死亡人数几乎无新增。
2.印度在2021年4月以前,死亡人数逐步上升,4月之后,死亡人数激增。毫无减弱的趋势。
3.美国死亡人数一直在增加,在2021年6增加的最多。
4.剩余的其他国家死亡人数也在一直激增。没有减弱的趋势。
3.全球死亡人数地理分布情况
fig = px.choropleth(latest_grouped, locations="country",
locationmode='country names', color="deaths",
hover_name="deaths", range_color=[1,100],
color_continuous_scale="peach",
title='Countries with Reported Deaths')
# fig.update(layout_coloraxis_showscale=False)
fig.show()
死亡人数地理分布图更直观的可以看出,南美洲、北美洲、欧洲的国家,印度、俄罗斯等国家的死亡人数居多。
4.死亡人数排名前十位的国家
fig = px.bar(latest_grouped.sort_values('deaths', ascending=False)[:10][::-1],
x='deaths', y='country',
title='Confirmed Deaths Worldwide', text='deaths', orientation='h')
fig.show()
5.美国死亡人数前十的州
fig = px.bar(usa_latest.sort_values('deaths', ascending=False)[:10][::-1],
x='deaths', y='state', color_discrete_sequence=['#D63230'],
title='Deaths in USA', text='deaths', orientation='h')
fig.show()
美国的加利福利亚州、纽约州和得克萨斯州的新冠死亡人数最多,其余州也有大部分的死亡人数。
6.中国大陆死亡人数排名前十的省市
fig = px.bar(china_latest.sort_values('deaths', ascending=False)[:10][::-1],
x='deaths', y='state', color_discrete_sequence=['#D63230'],
title='Deaths in China', text='deaths', orientation='h')
fig.show()
中国大陆的新冠死亡人数最多的省份是湖北省,其余省份死亡人数较少。