📜  Python – 使用 covid19 India API 进行数据可视化

📅  最后修改于: 2022-05-13 01:55:27.339000             🧑  作者: Mango

Python – 使用 covid19 India API 进行数据可视化

API(应用程序编程接口)是多个软件之间交互的计算接口。

JSON(JavaScript Object Notation)是一种用于存储和传输数据的轻量级格式。它用于将数据从服务器发送到网络。

所需模块:

  • matplotlib
  • 要求
  • 熊猫
  • json

安装模块的命令:

pip install matplotlib
pip install requests
pip install pandas

脚步:

  1. 导入所有必需的模块。
  2. 调用 API 并获取 JSON 数据。
  3. 获取 State Wise District 数据。
  4. 数据可视化。

以下 URL 将您重定向到 API https://api.covid19india.org/state_district_wise.json

导入所有需要的模块

Python3
#importing modules
import json
import requests
import pandas as pd
import matplotlib.pyplot as plt


Python3
#storing the url in the form of string
url="https://api.covid19india.org/state_district_wise.json"
 
#function to get data from api
def casesData():
    #getting the json data by calling api
    data = ((requests.get(url)).json())
    states = []


Python3
# getting statewise data
for state in states:
        f = (data[state]['districtData'])
# states data available in JSON Data
'''
0 State Unassigned
1 Andaman and Nicobar Islands
2 Andhra Pradesh
3 Arunachal Pradesh
4 Assam
5 Bihar
6 Chandigarh
7 Chhattisgarh
8 Delhi
9 Dadra and Nagar Haveli and Daman and Diu
10 Goa
11 Gujarat
12 Himachal Pradesh
13 Haryana
14 Jharkhand
15 Jammu and Kashmir
16 Karnataka
17 Kerala
18 Ladakh
19 Lakshadweep
20 Maharashtra
21 Meghalaya
22 Manipur
23 Madhya Pradesh
24 Mizoram
25 Nagaland
26 Odisha
27 Punjab
28 Puducherry
29 Rajasthan
30 Sikkim
31 Telangana
32 Tamil Nadu
33 Tripura
34 Uttar Pradesh
35 Uttarakhand
36 West Bengal
'''


Python3
# getting statewise data
for state in states:
    f = (data[state]['districtData'])
    tc = []
    dis = []
    act, con, dea, rec = 0, 0, 0, 0
 
    # getting districtwise data
    for key in (data[state]['districtData']).items():
        district = key[0]
        dis.append(district)
        active = data[state]['districtData'][district]['active']
        confirmed = data[state]['districtData'][district]['confirmed']
        deaths = data[state]['districtData'][district]['deceased']
        recovered = data[state]['districtData'][district]['recovered']
        if district == 'Unknown':
            active, confirmed, deaths, recovered = 0, 0, 0, 0
        tc.append([active, confirmed, deaths, recovered])
        act = act + active
        con = con + confirmed
        dea = dea + deaths
        rec = rec + recovered
    tc.append([act, con, dea, rec])
    dis.append('Total')
    parameters = ['Active', 'Confirmed', 'Deaths', 'Recovered']


Python3
# creating a dataframe
df = pd.DataFrame(tc, dis, parameters)
print('COVID - 19', state, 'District Wise Data')
print(df)


Python3
# plotting of data
plt.bar(dis, df['Active'], width=0.5, align='center')
fig = plt.gcf()
fig.set_size_inches(18.5, 10.5)
plt.xticks(rotation=75)
plt.show()
print('*'*100)


Python3
# function to get data from api
def casesData():
    # getting the json data by calling api
    data = ((requests.get(url)).json())
    states = []
 
    # getting states
    for key in data.items():
        states.append(key[0])
 
    # getting statewise data
    for state in states:
        f = (data[state]['districtData'])
        tc = []
        dis = []
        act, con, dea, rec = 0, 0, 0, 0
 
        # getting districtwise data
        for key in (data[state]['districtData']).items():
            district = key[0]
            dis.append(district)
            active = data[state]['districtData'][district]['active']
            confirmed = data[state]['districtData'][district]['confirmed']
            deaths = data[state]['districtData'][district]['deceased']
            recovered = data[state]['districtData'][district]['recovered']
            if district == 'Unknown':
                active, confirmed, deaths, recovered = 0, 0, 0, 0
            tc.append([active, confirmed, deaths, recovered])
            act = act + active
            con = con + confirmed
            dea = dea + deaths
            rec = rec + recovered
        tc.append([act, con, dea, rec])
        dis.append('Total')
        parameters = ['Active', 'Confirmed', 'Deaths', 'Recovered']
 
        # creating a dataframe
        df = pd.DataFrame(tc, dis, parameters)
        print('COVID - 19', state, 'District Wise Data')
        print(df)
 
        # plotting of data
        plt.bar(dis, df['Active'], width=0.5, align='center')
        fig = plt.gcf()
        fig.set_size_inches(18.5, 10.5)
        plt.xticks(rotation = 75)
        plt.show()
        print('*' * 100)


Python3
# importing modules
import json
import requests
import pandas as pd
import matplotlib.pyplot as plt
 
# storing the url in the form of string
url = "https://api.covid19india.org/state_district_wise.json"
 
# function to get data from api
 
 
def casesData():
    # getting the json data by calling api
    data = ((requests.get(url)).json())
    states = []
 
    # getting states
    for key in data.items():
        states.append(key[0])
 
    # getting statewise data
    for state in states:
        f = (data[state]['districtData'])
        tc = []
        dis = []
        act, con, dea, rec = 0, 0, 0, 0
 
        # getting districtwise data
        for key in (data[state]['districtData']).items():
            district = key[0]
            dis.append(district)
            active    = data[state]['districtData'][district]['active']
            confirmed = data[state]['districtData'][district]['confirmed']
            deaths    = data[state]['districtData'][district]['deceased']
            recovered = data[state]['districtData'][district]['recovered']
            if district == 'Unknown':
                active, confirmed, deaths, recovered = 0, 0, 0, 0
            tc.append([active, confirmed, deaths, recovered])
            act = act + active
            con = con + confirmed
            dea = dea + deaths
            rec = rec + recovered
        tc.append([act, con, dea, rec])
        dis.append('Total')
        parameters = ['Active', 'Confirmed', 'Deaths', 'Recovered']
 
        # creating a dataframe
        df = pd.DataFrame(tc, dis, parameters)
        print('COVID - 19', state, 'District Wise Data')
        print(df)
 
        # plotting of data
        plt.bar(dis, df['Active'], width = 0.5, align = 'center')
        fig = plt.gcf()
        fig.set_size_inches(18.5, 10.5)
        plt.xticks(rotation = 75)
        plt.show()
        print('*' * 100)
 
 
# states data available through API
'''
0 State Unassigned
1 Andaman and Nicobar Islands
2 Andhra Pradesh
3 Arunachal Pradesh
4 Assam
5 Bihar
6 Chandigarh
7 Chhattisgarh
8 Delhi
9 Dadra and Nagar Haveli and Daman and Diu
10 Goa
11 Gujarat
12 Himachal Pradesh
13 Haryana
14 Jharkhand
15 Jammu and Kashmir
16 Karnataka
17 Kerala
18 Ladakh
19 Lakshadweep
20 Maharashtra
21 Meghalaya
22 Manipur
23 Madhya Pradesh
24 Mizoram
25 Nagaland
26 Odisha
27 Punjab
28 Puducherry
29 Rajasthan
30 Sikkim
31 Telangana
32 Tamil Nadu
33 Tripura
34 Uttar Pradesh
35 Uttarakhand
36 West Bengal
'''
 
#Driver Code
casesData()


用于从 API 获取 JSON 数据和数据可视化的函数

蟒蛇3

#storing the url in the form of string
url="https://api.covid19india.org/state_district_wise.json"
 
#function to get data from api
def casesData():
    #getting the json data by calling api
    data = ((requests.get(url)).json())
    states = []

获取 JSON 数据中可用的状态名称

蟒蛇3

# getting statewise data
for state in states:
        f = (data[state]['districtData'])
# states data available in JSON Data
'''
0 State Unassigned
1 Andaman and Nicobar Islands
2 Andhra Pradesh
3 Arunachal Pradesh
4 Assam
5 Bihar
6 Chandigarh
7 Chhattisgarh
8 Delhi
9 Dadra and Nagar Haveli and Daman and Diu
10 Goa
11 Gujarat
12 Himachal Pradesh
13 Haryana
14 Jharkhand
15 Jammu and Kashmir
16 Karnataka
17 Kerala
18 Ladakh
19 Lakshadweep
20 Maharashtra
21 Meghalaya
22 Manipur
23 Madhya Pradesh
24 Mizoram
25 Nagaland
26 Odisha
27 Punjab
28 Puducherry
29 Rajasthan
30 Sikkim
31 Telangana
32 Tamil Nadu
33 Tripura
34 Uttar Pradesh
35 Uttarakhand
36 West Bengal
'''

获取状态数据

蟒蛇3

# getting statewise data
for state in states:
    f = (data[state]['districtData'])
    tc = []
    dis = []
    act, con, dea, rec = 0, 0, 0, 0
 
    # getting districtwise data
    for key in (data[state]['districtData']).items():
        district = key[0]
        dis.append(district)
        active = data[state]['districtData'][district]['active']
        confirmed = data[state]['districtData'][district]['confirmed']
        deaths = data[state]['districtData'][district]['deceased']
        recovered = data[state]['districtData'][district]['recovered']
        if district == 'Unknown':
            active, confirmed, deaths, recovered = 0, 0, 0, 0
        tc.append([active, confirmed, deaths, recovered])
        act = act + active
        con = con + confirmed
        dea = dea + deaths
        rec = rec + recovered
    tc.append([act, con, dea, rec])
    dis.append('Total')
    parameters = ['Active', 'Confirmed', 'Deaths', 'Recovered']

使用 Pandas 创建 DataFrame

蟒蛇3

# creating a dataframe
df = pd.DataFrame(tc, dis, parameters)
print('COVID - 19', state, 'District Wise Data')
print(df)

使用 Matplotlib 进行数据可视化

蟒蛇3

# plotting of data
plt.bar(dis, df['Active'], width=0.5, align='center')
fig = plt.gcf()
fig.set_size_inches(18.5, 10.5)
plt.xticks(rotation=75)
plt.show()
print('*'*100)

最后的casesData()函数代码

蟒蛇3

# function to get data from api
def casesData():
    # getting the json data by calling api
    data = ((requests.get(url)).json())
    states = []
 
    # getting states
    for key in data.items():
        states.append(key[0])
 
    # getting statewise data
    for state in states:
        f = (data[state]['districtData'])
        tc = []
        dis = []
        act, con, dea, rec = 0, 0, 0, 0
 
        # getting districtwise data
        for key in (data[state]['districtData']).items():
            district = key[0]
            dis.append(district)
            active = data[state]['districtData'][district]['active']
            confirmed = data[state]['districtData'][district]['confirmed']
            deaths = data[state]['districtData'][district]['deceased']
            recovered = data[state]['districtData'][district]['recovered']
            if district == 'Unknown':
                active, confirmed, deaths, recovered = 0, 0, 0, 0
            tc.append([active, confirmed, deaths, recovered])
            act = act + active
            con = con + confirmed
            dea = dea + deaths
            rec = rec + recovered
        tc.append([act, con, dea, rec])
        dis.append('Total')
        parameters = ['Active', 'Confirmed', 'Deaths', 'Recovered']
 
        # creating a dataframe
        df = pd.DataFrame(tc, dis, parameters)
        print('COVID - 19', state, 'District Wise Data')
        print(df)
 
        # plotting of data
        plt.bar(dis, df['Active'], width=0.5, align='center')
        fig = plt.gcf()
        fig.set_size_inches(18.5, 10.5)
        plt.xticks(rotation = 75)
        plt.show()
        print('*' * 100)

最终实现:

蟒蛇3

# importing modules
import json
import requests
import pandas as pd
import matplotlib.pyplot as plt
 
# storing the url in the form of string
url = "https://api.covid19india.org/state_district_wise.json"
 
# function to get data from api
 
 
def casesData():
    # getting the json data by calling api
    data = ((requests.get(url)).json())
    states = []
 
    # getting states
    for key in data.items():
        states.append(key[0])
 
    # getting statewise data
    for state in states:
        f = (data[state]['districtData'])
        tc = []
        dis = []
        act, con, dea, rec = 0, 0, 0, 0
 
        # getting districtwise data
        for key in (data[state]['districtData']).items():
            district = key[0]
            dis.append(district)
            active    = data[state]['districtData'][district]['active']
            confirmed = data[state]['districtData'][district]['confirmed']
            deaths    = data[state]['districtData'][district]['deceased']
            recovered = data[state]['districtData'][district]['recovered']
            if district == 'Unknown':
                active, confirmed, deaths, recovered = 0, 0, 0, 0
            tc.append([active, confirmed, deaths, recovered])
            act = act + active
            con = con + confirmed
            dea = dea + deaths
            rec = rec + recovered
        tc.append([act, con, dea, rec])
        dis.append('Total')
        parameters = ['Active', 'Confirmed', 'Deaths', 'Recovered']
 
        # creating a dataframe
        df = pd.DataFrame(tc, dis, parameters)
        print('COVID - 19', state, 'District Wise Data')
        print(df)
 
        # plotting of data
        plt.bar(dis, df['Active'], width = 0.5, align = 'center')
        fig = plt.gcf()
        fig.set_size_inches(18.5, 10.5)
        plt.xticks(rotation = 75)
        plt.show()
        print('*' * 100)
 
 
# states data available through API
'''
0 State Unassigned
1 Andaman and Nicobar Islands
2 Andhra Pradesh
3 Arunachal Pradesh
4 Assam
5 Bihar
6 Chandigarh
7 Chhattisgarh
8 Delhi
9 Dadra and Nagar Haveli and Daman and Diu
10 Goa
11 Gujarat
12 Himachal Pradesh
13 Haryana
14 Jharkhand
15 Jammu and Kashmir
16 Karnataka
17 Kerala
18 Ladakh
19 Lakshadweep
20 Maharashtra
21 Meghalaya
22 Manipur
23 Madhya Pradesh
24 Mizoram
25 Nagaland
26 Odisha
27 Punjab
28 Puducherry
29 Rajasthan
30 Sikkim
31 Telangana
32 Tamil Nadu
33 Tripura
34 Uttar Pradesh
35 Uttarakhand
36 West Bengal
'''
 
#Driver Code
casesData()

输出: