Covid-19 Data Tracking with Smartable AI

in #python4 years ago (edited)

Smartable AI coronavirus API offers the latest data related pandemic per country and it is completely free to use. While working with data manipulation issues, I built a simple Covid-19 Data Tracking with Python.

Screenshot_1.png

It is quite convenient to import some libraries at the beginning.

import pandas as pd
import matplotlib.pyplot as plt
import requests
import json

It is possible to create various graphics by using the simple functions of matplotlib to create DataFrames with pandas and visualize the data you want. After importing libraries call your API from the URL. You will need to sign up for the site to get a subscription key.

In the code, I used iloc function to slice unnecessary data. Since the case start date is different for each region, some action is also required at this point.

def getStats(country):
    api_url = 'https://api.smartable.ai/coronavirus/stats/' + country
    api_params = {
        'Cache-Control': 'no-cache',
        'Subscription-Key': '6de776cad46d41dd96317325d23c4ee0',
    }
    r = requests.get(url=api_url, params=api_params)
    return r.text

data = getStats('TR')

jsonData = json.loads(data)
jsonData.keys()

country = jsonData["location"]["countryOrRegion"]
updatedDateTime = jsonData["updatedDateTime"]
print(country, updatedDateTime)

history = pd.DataFrame(jsonData["stats"]["history"])
history["date"] = pd.to_datetime(history["date"])

df = history.iloc[49:]
df.index = np.arange(1, len(df) + 1)


df.plot(figsize=(15, 7.5), x="date", title=country, grid=True, )

plt.show()


I want to convert the x-axis to time series and change its format. If there is someone who can help, I would appreciate it if you write comments.

The API is RESTful and returns data formatted in JSON.

Data Source: https://developer.smartable.ai/

Sort:  

This post was shared in the Curation Collective Discord community for curators, and upvoted and reblogged by the @c-squared community account after manual review.
@c-squared runs a community witness. Please consider using one of your witness votes on us here

I have picked your post for my daily hive voting initiative, Keep it up and Hive On!!