• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

python 搞代码 4年前 (2022-01-09) 22次浏览 已收录 0个评论
文章目录[隐藏]

一、安装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,
  <strong>本文来源gaodai#ma#com搞@@代~&码*网2</strong>              locations='Country',
                locationmode='country names',
                color='vaccines',
                facet_col='vaccines',
                facet_col_wrap=3)
fig.update_layout(width=1200, height=1000)
fig.show()

六、组织宽表

#组织宽表
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()

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python实战之疫苗研发情况可视化
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址