How to collect Covid19 Data using API in Python

In this tutorial, we will be collecting covid19 Data using API in Python.

What is API?

API (Application Programming Interface) is a computing interface that interacts between multiple software. 

What is JSON?

JSON (JavaScript Object Notation) is a lightweight format for storing and transporting data. It is used to send data from server to web.

Required modules: 

  • matplotlib
  • requests
  • pandas
  • json
Commands to install modules:
pip install matplotlib
pip install requests
pip install pandas

Ignore this section if you’ve already installed these modules.

Steps:

  1. Importing all required modules.
  2. Calling API and getting JSON data.
  3. Getting the Data for a particular state
  4. Visualization of data.

The below URL redirects you to API “https://data.covid19india.org/states_daily.json

Importing all required modules

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

Function for getting JSON data from API and Visualization of Data



states = data['states_daily']

confirmed_cases = []
recovered_cases = []
deceased_cases = []
date=[]
for i in range(len(states)):
    date.append(states[i]['dateymd'])
  

for i in range(len(states)):
    if states[i]['status'] == 'Confirmed':
        confirmed_cases.append(states[i]['dl'])
    elif states[i]['status'] == 'Recovered':
        recovered_cases.append(states[i]['dl'])
    else:
        deceased_cases.append(states[i]['dl'])

Creating DataFrame Using pandas

df=pd.DataFrame(list(zip(date,confirmed_cases,recovered_cases,deceased_cases)),columns =['Date','confirmed_cases','recovered_cases','deceased_cases'])
df.head()

Casting the data type of All the Attributes

df["Date"]=pd.to_datetime(df["Date"])
df["confirmed_cases"]=pd.to_numeric(df["confirmed_cases"])
df["recovered_cases"]=pd.to_numeric(df["recovered_cases"])
df["deceased_cases"]=pd.to_numeric(df["deceased_cases"])
df = df.set_index(df["Date"])
df=df.drop(columns=['Date'])
df.head()
df.describe()
import matplotlib.pyplot as plt
plt.style.use("fivethirtyeight")
df.plot(subplots=True, figsize=(12, 15))
array([<matplotlib.axes._subplots.AxesSubplot object at 0x000002B3C2941550>,
       <matplotlib.axes._subplots.AxesSubplot object at 0x000002B3C3026C18>,
       <matplotlib.axes._subplots.AxesSubplot object at 0x000002B3C305BFD0>],
      dtype=object)

Conclusion

In this way, we can find the covid19 data for a particular state. You can change the state code and get the data for your state. If you find any difficulty in following the tutorial, mention them in the comment section.

Important Notice for college students

If you’re a college student and have skills in programming languages, Want to earn through blogging? Mail us at geekycomail@gmail.com

For more Programming related blogs Visit Us Geekycodes. Follow us on Instagram.

By geekycodesco

Leave a Reply

%d bloggers like this: