How to track Corona Virus statistics in Python

Hello Everyone! Life has been hard lately. We are living through tough times. So why not do something about it? Here’s my take on ……How to track Corona Virus Statistics in Python using COVID19Py.

As we all know one of the most useful features of Python is the huge number of functions and libraries available. And adding to that we have an unofficial library of Covid19Py at our disposal now. It is a ready-made Corona Virus Stats tracker in Python. All you have to do is install it, run some functions and all the details all over the globe is at your fingertips. So let’s get to it.

Installing COVID19Py

Installation in Python is as easy as it gets. Just run the following command in your command prompt.

pip install COVID19Py

In case this doesn’t work, try using –user at the end to avoid security issues, especially in Windows OS.

Importing and Preprocessing

To import this package into your .py file type in the following command :

import COVID19Py

There is just one line of preprocessing in this package making it very easy to use.

covid19 = COVID19Py.COVID19()

Finding the latest information

Now that we have an object of the package we can start using its methods.

Use the getLatest() method to collect the most relevant information about affected, recovered and the number of deaths all over the world. It returns a list of dictionaries.

latest = covid19.getLatest()
print(latest)

Next, to sort through the huge volume of data obtained, use the getLocations() method.

locations = covid19.getLocations()
print(locations)

To search through the data for one particular country, you need the country code. So here is a quick way to get hold of all the country codes in the package.

countries = {}
for i in locations:
    countries[i.get('country')] = i.get('country_code')

Now to find the statistics for India, simply put in this piece of code.

code = countries.get('India')
india = covid19.getLocationByCountryCode("IN")
for i in india:
    print(i.get("latest"))

Output :

{'confirmed': 13430, 'deaths': 437, 'recovered': 1749}

Now that you have all the resources at hand, find all the data you need and you can compare and contrast between different the stats of different countries.

To see the trend of Corona Virus all over the internet, you may also use the Google Trends API. You can find a relevant article on that here: How to track Google trends in Python using Pytrends

For more details on the package, please visit: https://pypi.org/project/COVID19Py/

I hope this was helpful. Stay Safe, everyone.

 

Leave a Reply