使用Python实现国内疫情数据地图

本篇博客将实现国内疫情数据地图

爬取数据

import requests
import json
china_url="https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5"

headers={
    "User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36",
    "Referer":"https://news.qq.com/"
}
#获取到json数据
response=requests.get(url=china_url,headers=headers).json()
#先把数据改成Python中的字典
data=json.loads(response['data'])

print(response)
#保存数据
with open('./国内疫情.json','w') as f:
    #再把字典改成json字符串
    f.write(json.dumps(data,ensure_ascii=False,indent=2))

将json数据保存到excel中

import pandas as pd
import json
#将json数据转存到excel中
#读取文件
with open("./国内疫情.json",'r',encoding="utf-8") as f:
    data=f.read()
data=json.loads(data)
#获取国内所有数据
chinaAreaDict=data['areaTree'][0]
provinceList=chinaAreaDict['children']
# print(len(provinceList))
china_citylist=[]#{{湖北,武汉},{湖北,襄阳}}
for x in range(len(provinceList)):
    province=provinceList[x]['name']
    province_List=provinceList[x]['children']
    for y in range(len(province_List)):
        city=province_List[y]['name']
        today=province_List[y]['today']
        total=province_List[y]['total']
        city_dict={'province':province,"city":city,'today':today,'total':total}
        china_citylist.append(city_dict)
chinaTotalData=pd.DataFrame(china_citylist)

#将chinaTotalData中的today和total数据添加到DataFrame中
confirmlist=[]
suspectlist=[]
deadlist=[]
heallist=[]
deadRatelist=[]
healRatelist=[]
for value in chinaTotalData['total'].values.tolist():
    confirmlist.append(value['confirm'])
    suspectlist.append(value['suspect'])
    deadlist.append(value['dead'])
    heallist.append(value['heal'])
    deadRatelist.append(value['deadRate'])
    healRatelist.append(value['healRate'])


chinaTotalData['confirm']=confirmlist
chinaTotalData['suspect']=suspectlist
chinaTotalData['dead']=deadlist
chinaTotalData['heal']=heallist
chinaTotalData['deadRate']=deadRatelist
chinaTotalData['healRate']=healRatelist

# 拆分today列
today_confirmlist=[]
today_confirmCutslist=[]
for value in chinaTotalData['today'].values.tolist():
    today_confirmlist.append(value['confirm'])
    today_confirmCutslist.append(value['confirmCuts'])

chinaTotalData['today_confirm']=today_confirmlist
chinaTotalData['today_confirmCutslist']=today_confirmCutslist


chinaTotalData.drop(['total','today'],axis=1,inplace=True)
chinaTotalData

#将其保存到excel中
from openpyxl import load_workbook
book=load_workbook('国内疫情.xlsx')
writer=pd.ExcelWriter('国内疫情.xlsx',engine='openpyxl')
writer.book=book
writer.sheets=dict((ws.title,ws) for ws in book.worksheets)
chinaTotalData.to_excel(writer,index=False)
writer.save()
writer.close()

使用pycharts实现国内疫情地图

import pandas as pd
from pyecharts import options as opts 
from pyecharts.charts import Map
df = pd.read_excel('./国内疫情.xlsx')
#根据绘制国内总疫情图
data=df.groupby(by='province',as_index=False).sum()
data_list = list(zip(data['province'].values.tolist(),data['confirm'].values.tolist()))
def map_china() -> Map:
    c=(
        Map()
        .add(series_name="确诊病例",data_pair=data_list,maptype='china')
        .set_global_opts(
            title_opts=opts.TitleOpts(title="疫情地图"),
            visualmap_opts=opts.VisualMapOpts(is_piecewise=True,
                        pieces=[{"max": 9, "min": 0,"label": "0-9", "color": "#FFE4E1"},
                                {"max": 99, "min": 10, "label": "10-99", "color": "#FF7F50"},
                                {"max": 499, "min": 100, "label": "100-499", "color": "#F08080"},
                                {"max": 999, "min": 500, "label": "500-999", "color": "#CD5C5C"},
                                {"max": 99999, "min": 1000, "label": ">=1000", "color": "#990000"}]
                                    )
        )
    )
    return c
d_map=map_china()
d_map.render_notebook()

实现效果如下

使用Python实现国内疫情数据地图_第1张图片

本demo注意事项

关于pyecharts的使用注意事项及所需安装的库

python 使用openpyxl模块时报错zipfile.BadZipFile: File is not a zip file的解决方法

你可能感兴趣的:(echarts,python,python,数据可视化,excel,json)