This repository has been archived by the owner on Oct 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_data.py
150 lines (116 loc) · 4.54 KB
/
get_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# -*- coding: utf-8 -*-
"""Untitled3.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1Z1uf7sLgFU1LkGshJLYo7UZEXENXH6Gh
"""
import json
import pandas as pd
import numpy as np
import os
import datetime
os.system("curl -O https://api.rootnet.in/covid19-in/stats/hospitals");
os.system("curl -O https://api.rootnet.in/covid19-in/stats/daily")
os.system("curl -O https://api.rootnet.in/covid19-in/contacts")
with open('hospitals') as json_file:
data = json.load(json_file)
df = pd.DataFrame(columns=['state', 'ruralHospitals', 'ruralBeds', 'urbanHospitals', 'urbanBeds', 'totalHospitals', 'totalBeds'])
for item in data['data']['regional']:
df = df.append(item, ignore_index=True)
with open('daily') as f:
data = json.load(f)
max_date = data['data'][-1]['day']
for item in data['data']:
day = datetime.datetime.strptime(item['day'], '%Y-%m-%d').strftime("%B %d")
for reg_item in item['regional']:
if not day in df.columns:
df[day] = 0
df.loc[df['state'] == reg_item['loc'], day] = reg_item['confirmedCasesIndian'] + reg_item['confirmedCasesForeign']
df['Final'] = df[datetime.datetime.strptime(max_date, '%Y-%m-%d').strftime("%B %d")]
df['hospitalBeds'] = df['totalBeds']
with open('contacts') as f:
data = json.load(f)
df['helpline'] = 1075
for item in data['data']['contacts']['regional']:
df.loc[df['state'] == item['loc'], 'helpline'] = item['number']
df = df.fillna(0)
index = df[df['state'] == 'INDIA'].index
df.drop(index , inplace=True)
df = df.rename(columns={
"state": "Name",
"ruralHospitals": "Rural Hospitals",
"ruralBeds": "Rural Beds",
"urbanHospitals": "Urban Hospitals",
"urbanBeds": "Urban Beds",
"totalHospitals": "Total Hospitals",
"hospitalBeds": "Hospital Beds",
"confirmed": "Confirmed Cases",
"recovered": "Recovered Cases",
"deaths": "Deaths",
"helpline": "Helpline"
})
for index, row in df.iterrows():
if '&'in row['Name']:
df.loc[index, 'Name'] = row['Name'].replace('&', 'and')
for index, row in df.iterrows():
if '&'in str(row['Helpline']):
df.loc[index, 'Helpline'] = str(row['Helpline']).replace('+91-', '0')
if ',' in str(row['Helpline']):
df.loc[index, 'Helpline'] = str(df.loc[index, 'Helpline']).split(',')[0]
df.to_csv('india_states_daily.csv', index=False)
"""## For map data"""
df = pd.DataFrame(columns=['state',
'ruralHospitals', 'ruralBeds',
'urbanHospitals', 'urbanBeds',
'totalHospitals', 'totalBeds',
'confirmed', 'recovered', 'deaths', 'date'])
with open('hospitals') as json_file:
data = json.load(json_file)
india = data['data']['summary']
india['state'] = 'India'
df = df.append(india, ignore_index=True)
for item in data['data']['regional']:
df = df.append(item, ignore_index=True)
df.head()
with open('daily') as f:
data = json.load(f)
item = data['data'][-1]
day = datetime.datetime.strptime(item['day'], '%Y-%m-%d').strftime("%B %d")
index = df['state'] == 'India'
df.loc[index, 'state'] = 'India'
df.loc[index, 'confirmed'] = item['summary']['total'],
df.loc[index, 'deaths'] = item['summary']['deaths']
df.loc[index, 'recovered'] = item['summary']['discharged'],
df.loc[index, 'date'] = day
for reg_item in item['regional']:
index = df['state'] == reg_item['loc']
df.loc[index, 'state'] = reg_item['loc']
df.loc[index, 'confirmed'] = reg_item['confirmedCasesIndian'] + reg_item['confirmedCasesForeign']
df.loc[index, 'deaths'] = reg_item['deaths']
df.loc[index, 'recovered'] = reg_item['discharged']
df.loc[index, 'date'] = day
df.head()
with open('contacts') as f:
data = json.load(f)
contacts = data['data']
df['helpline'] = 0
df.loc[df['state'] == 'India', 'helpline'] = data['data']['contacts']['primary']['number-tollfree']
for item in data['data']['contacts']['regional']:
df.loc[df['state'] == item['loc'], 'helpline'] = item['number']
df = df.fillna(0)
df = df.rename(columns={
"state": "Name",
"ruralHospitals": "Rural Hospitals",
"ruralBeds": "Rural Beds",
"urbanHospitals": "Urban Hospitals",
"urbanBeds": "Urban Beds",
"totalHospitals": "Total Hospitals",
"totalBeds": "Total Beds",
"confirmed": "Confirmed Cases",
"recovered": "Recovered Cases",
"Deaths": "Deaths"
})
for index, row in df.iterrows():
if '&'in row['Name']:
df.loc[index, 'Name'] = row['Name'].replace('&', 'and')
df.to_csv('india_states.csv', index=False)