如何使用 pd.read_html 将实时数据导入 python,例如 Corona 的数据?

How to import live data into python using pd.read_html such as that of Corona?

我是 python 的新手,想从事数据绘图工作。我想使用 google 数据或世界仪表绘制 COVID-19 的实时数据。我正在使用 pandas 和 plotly_graph_objs。

我应该如何将实时数据导入代码以便进一步将其用于 Choropleth?

import numpy as np
import pandas as pd
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode,iplot
init_notebook_mode(connected = True)

df = pd.read_csv('owid-covid-data.csv')
df.head()
data = dict(type='choropleth',
           colorscale = 'Viridis',
           reversescale = True,
           locations = df['location'],
           locationmode = 'country names',
           z = df['new_cases'],
            #zz = data['total_deaths_per_million'],
           text = df['iso_code'],
           colorbar = {'title':'COVID'})

layout = dict(title='COVID-19',
             geo = dict(showframe=True,
                       projection = {'type':'mercator'}))

choromap = go.Figure(data=[data],layout=layout)
iplot(choromap,validate=False)

最简单的方法是从 Johns Hopkins CSSE 在 Github 的 Data Repository 获取每日病毒统计数据:https://github.com/CSSEGISandData/COVID-19

替代方法是解析来自 WorldMeters 或类似网站的数据。

另请查看 resources at Kaggle

先获取数据,然后使用适合您数据的方法读入 DataFrame。

例如

import requests

countries = requests.get('https://www.worldometers.info/coronavirus/#countries')
df = pd.read_html(countries.text)[-1]

df

Country,Other   TotalCases  NewCases    TotalDeaths NewDeaths   TotalRecovered  ActiveCases Serious,Critical    Tot Cases/1M pop    Deaths/1M pop   TotalTests  Tests/ 1M pop
0   World   2181308 +95,022 145471.0    +6,996  547069.0    1488768 56602.0 280.00  18.7    NaN NaN
1   China   82341   +46 3342.0  NaN 77892.0 1107    95.0    57.00   2.0 NaN NaN
2   USA 677570  +29,567 34617.0 +2,174  57508.0 585445  13369.0 2047.00 105.0   3398140.0   10266.0
3   Spain   184948  +4,289  19315.0 +503    74797.0 90836   7371.0  3956.00 413.0   930230.0    19896.0
4   Italy   168941  +3,786  22170.0 +525    40164.0 106607  2936.0  2794.00 367.0   1178403.0   19490.0
... ... ... ... ... ... ... ... ... ... ... ... ...
209 British Virgin Islands  3   NaN NaN NaN 2.0 1   NaN 99.00   NaN NaN NaN
210 Caribbean Netherlands   3   NaN NaN NaN NaN 3   NaN 114.00  NaN 10.0    381.0
211 Saint Pierre Miquelon   1   NaN NaN NaN NaN 1   NaN 173.00  NaN NaN NaN
212 Yemen   1   NaN NaN NaN NaN 1   NaN 0.03    NaN NaN NaN
213 Total:  2181308 +95,022 145471.0    +6,996  547069.0    1488768 56602.0 279.80  18.7    NaN NaN
214 rows × 12 columns

不确定 Google 数据,他们是否提供 API 或者您需要抓取数据。