之前說到過,和同事們做了一個免費開源的新冠數據訪問接口項目:COVID-19-Data-API。今天開始我們用 Python 、jupyter notebook、pandas等來進行數據分析的一個教程。
疫情過後,有人說剩下來的都是數字化的企業了,數據分析能力也已經是一項必備技能了,有太多的工具可以進行數據分析,前幾年可能excel 還是很多人認為最有效的數據分析工具,現在可以選擇面太廣了,免費工具裡面,Python+pandas 肯定還是最佳選擇之一。準備一套完整的 Python 的入門教材+Python數據分析教程,並且用現在時髦的視頻方式錄製。所以先準備好教材。
下面的代碼從接口處獲得數據,經過簡單的整理,輸出。
具體來說,是獲得意大利在2020年3月24日的疫情感染數據。
1 設置API的地址,調用token
2 設置headers、payload等需要調用的參數
3 通過 requests 的 get 方法來訪問數據
4 通過 pandas 來簡單處理數據
5 顯示數據
# demo for infection/region
# input region, start_date, get data
# 接口:感染/國家地區
import requests
import pandas as pd
# API url
url = 'https://covid-19.adapay.tech/api/v1/'
# token, can call register function get the API token
token = '497115d0c2ff9586bf0fe03088cfdbe2'
# region or country
region='Italy'
# headers, need the API token
headers = {
'token': token
}
# the params
payload = {
'region': region,
'start_date':'2020-03-24'
}
# call requets to load
r = requests.get(url+'infection/region', params=payload, headers=headers)
data = r.json()
# use pandas to get the data
df = pd.DataFrame.from_dict(data['data']['region'][region])
print(df)
print('---')
通過requests 獲得數據,然後pandas 整理。輸出結果如下:
2020-03-24
confirmed 69176
confirmed_add 5249
deaths 6820
deaths_add 743
recovered 8326
recovered_add 894
即便你對Python不太懂,或者一知半解,相信看到上面的代碼也能夠猜的七七八八,Python的可讀性非常好。
要運行上面代碼,最簡單的方式是通過 jupyter-notebook,然後推薦下載安裝 anaconda,最強的 Python 擴展程序,下載安裝 anaconda 之後,直接運行 jupyter-notebook,就可以在notebook 裡面的 cell 單元格中輸入上面的代碼。 已經有很多教程關於怎麼使用 Python 下的 jupyter-notebook,可以先網上搜索一下,這裡就不贅述了。
下面的例子讀取一段時間範圍內的數據,並對行列進行交換,便於分析和製圖。基礎代碼延續之前的例子,所以要在上面運行的基礎上繼續。
# demo for infection/region
# input region, start_date, end_date, get data
# the params
payload = {
'region': region,
'start_date': '2020-03-24',
'end_date': '2020-03-31'
}
# call requets to load
r = requests.get(url+'infection/region', params=payload, headers=headers)
data = r.json()
# use pandas to get the data
df = pd.DataFrame.from_dict(data['data']['region'][region])
print(df)
print('---')
我們可以得到下面的結果:
2020-03-24 2020-03-25 2020-03-26 2020-03-27 2020-03-28 \
confirmed_add 5249 5210 6203 5909 5974
deaths_add 743 683 712 919 889
recovered_add 894 1036 999 589 1434
confirmed 69176 74386 80589 86498 92472
deaths 6820 7503 8215 9134 10023
recovered 8326 9362 10361 10950 12384
2020-03-29 2020-03-30 2020-03-31
confirmed_add 5217 4050 4053
deaths_add 756 812 837
recovered_add 646 1590 1109
confirmed 97689 101739 105792
deaths 10779 11591 12428
recovered 13030 14620 15729
我們把日期和確證等交換一下行列,便於製圖
# demo for infection/region
# input region, start_date, end_date, get data
# exchange the row and column by Pandas, the row index is date
# 交換數據的行和列
df = df.T
print(df)
print('---')
可以得到下面的結果:
confirmed_add deaths_add recovered_add confirmed deaths \
2020-03-24 5249 743 894 69176 6820
2020-03-25 5210 683 1036 74386 7503
2020-03-26 6203 712 999 80589 8215
2020-03-27 5909 919 589 86498 9134
2020-03-28 5974 889 1434 92472 10023
2020-03-29 5217 756 646 97689 10779
2020-03-30 4050 812 1590 101739 11591
2020-03-31 4053 837 1109 105792 12428
recovered
2020-03-24 8326
2020-03-25 9362
2020-03-26 10361
2020-03-27 10950
2020-03-28 12384
2020-03-29 13030
2020-03-30 14620
2020-03-31 15729