Last active
March 28, 2020 15:10
-
-
Save afandilham/b5b828a260c093ff95600297abbbc092 to your computer and use it in GitHub Desktop.
A simple script that turns kawalcorona's public API data into table python and save it as csv based on indonesia province.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import pandas as pd | |
request = requests.get('https://api.kawalcorona.com/indonesia/provinsi') | |
# data = request.json() | |
data = json.loads(request.text) | |
# print(json.dumps(data, indent=2)) | |
# Table for Covid-19 cases | |
table = [] | |
headers = ['Kode Provinsi', 'Provinsi', 'Positif', 'Sembuh', 'Meninggal'] | |
# varaible for storing value from API | |
kode_provinsi = [] | |
provinsi = [] | |
qty_kasus = [] | |
qty_sembuh = [] | |
qty_meninggal = [] | |
# Get the datas | |
for getData in data: | |
# Get datas and store to variables | |
kode_provinsi.append(getData['attributes']['Kode_Provi']) | |
provinsi.append(getData['attributes']['Provinsi']) | |
qty_kasus.append(getData['attributes']['Kasus_Posi']) | |
qty_sembuh.append(getData['attributes']['Kasus_Semb']) | |
qty_meninggal.append(getData['attributes']['Kasus_Meni']) | |
# store in table variable | |
storeData = { | |
"Kode Provinsi": kode_provinsi, | |
"Provinsi": provinsi, | |
"Positif": qty_kasus, | |
"Sembuh": qty_sembuh, | |
"Meninggal": qty_meninggal | |
} | |
df = pd.DataFrame(storeData) | |
# Save as csv dataset | |
df.to_csv(r'export_data.csv', index=False, header=True) | |
print(df) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment