首页 > 编程技术 > python

Python实战之疫苗研发情况可视化

发布时间:2021-5-19 00:00

一、安装plotly库

因为这部分内容主要是用plotly库进行数据动态展示,所以要先安装plotly库

pip install plotly

除此之外,我们对数据的处理还用了numpypandas库,如果你没有安装的话,可以用以下命令一行安装

pip install plotly numpy pandas

#导入所需库
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go

二、疫苗研发情况

各国采用的疫苗品牌概览

通过对各国卫生部门确认备案的疫苗品牌,展示各厂商的疫苗在全球的分布

#读取数据
locations=pd.read_csv(r'data/locations.csv')

locations

在这里插入图片描述

这里我们的loacation中可以看到各个地方的疫苗和数据的来源与数据来源的网页

三、数据处理

#发现数据中vaccines列中包含了多个品牌的情况,将这类数拆为多条
vaccines_by_country=pd.DataFrame()
for i in locations.iterrows():
    df=pd.DataFrame({'Country':i[1].location,'vaccines':i[1].vaccines.split(',')})
    vaccines_by_country=pd.concat([vaccines_by_country,df])
vaccines_by_country['vaccines']=vaccines_by_country.vaccines.str.strip()# 去掉空格

vaccines_by_country.vaccines.unique() # 查看疫苗的种类

在这里插入图片描述

四、可视化疫苗的分布情况

#绘图
fig=px.choropleth(vaccines_by_country,
                locations='Country',
                locationmode='country names',
                color='vaccines',
                facet_col='vaccines',
                facet_col_wrap=3)
fig.update_layout(width=1200, height=1000)
fig.show()

在这里插入图片描述

各品牌分布:

综上可以发现,全球采用最广的仍是Pfizer/BioNTech,国产疫苗中Sinovac(北京科兴疫苗)输出到了较多国家

五、各品牌疫苗上市情况(仅部分国家)

根据数据集中提供的部分国家20年12月以来各品牌疫苗接种情况,分析各品牌上市时间及市场占有情况

#读取数据
vacc_by_manu=pd.read_csv(r'data/vaccinations-by-manufacturer.csv')

#定义函数,用于从原始数据中组织宽表
def query(df,country,date,vaccine):
    try:
        result=df.loc[(df.location==country)&(df.date==date)&(df.vaccine==vaccine)].total_vaccinations.iloc[0]
    except:
        result=np.nan
    return result

vacc_by_manu

在这里插入图片描述

六、组织宽表

#组织宽表
vacc_combined=pd.DataFrame(columns=['location','date','Pfizer/BioNTech', 'Sinovac', 'Moderna', 'Oxford/AstraZeneca'])
for i in vacc_by_manu.location.unique():
    for j in vacc_by_manu.date.unique():
        for z in vacc_by_manu.vaccine.unique():
            result=query(vacc_by_manu,i,j,z)
            if vacc_combined.loc[(vacc_combined.location==i)&(vacc_combined.date==j)].empty:
                result_df=pd.DataFrame({'location':i,'date':j,z:result},index=['new'])
                vacc_combined=pd.concat([vacc_combined,result_df])
            else:
                vacc_combined.loc[(vacc_combined.location==i)&(vacc_combined.date==j),z]=result

vacc_combined

在这里插入图片描述

七、补全缺失数据

#补全缺失数据
temp=pd.DataFrame()
for i in vacc_combined.location.unique():#按国家进行不全
    r=vacc_combined.loc[vacc_combined.location==i]
    r=r.fillna(method='ffill',axis=0)#先按最近一次的数据进行补全
    temp=pd.concat([temp,r])#若没有最近的数据,认为该项为0
temp=temp.fillna(0).reset_index(drop=True)
temp

在这里插入图片描述

八、绘制堆叠柱状图

#绘制堆叠柱状图
fig=px.bar(temp,
        x='location',
        y=vacc_by_manu.vaccine.unique(),
        animation_frame='date',
        color_discrete_sequence=['#636efa','#19d3f3','#ab63fa','#00cc96']#为了查看方便,品牌颜色与前一部分对应
        )
fig.show()

在这里插入图片描述

数据中主要涉及Pfizer/BioNTech、Sinovac、Moderna、Oxford/AstraZeneca 4个品牌,其中:

到此这篇关于Python实战之疫苗研发情况可视化的文章就介绍到这了,更多相关Python疫苗研发情况可视化内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!

标签:[!--infotagslink--]

您可能感兴趣的文章: