COVID-19 Data Visualization using Python | by Jaskeerat Singh Bhatia | Towards Data Science

Data Visualization is the first step towards getting an insight into a large data set in every data science project. Once the data has been acquired and preprocessed (cleaned and deduplicated), the next step in the Data Science Life Cycle is Exploratory Data Analysis which kicks off with visualization of the data. The aim here is to extract useful information from the data.
I have used Python and its few powerful libraries to achieve the task. Also, I have used Google Colab Notebooks to write the code so as to avoid the hassle of installing any IDE or packages in case you wish to follow along.
The first step is to open a new Google Colab ipython notebook and import the libraries we require.
import pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport seaborn as snsimport plotly.express as px ### for plotting the data on world map
These visualizations are based on data as of May 25, 2020. I have used the daily report data published by John Hopkins University for May 25, 2020. The next part of the code deals with loading the .csv data to our project.
path = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/05-25-2020.csv'df = pd.read_csv(path)df.info()df.head()
In just two lines in code we have our data loaded and ready for use as a Pandas Dataframe. the next two lines display the information about the data (metadata) i.e., a total of 3409 rows of data and 11 columns. It also gives us a preview of the first five rows.
Now since our data has loaded successfully, the next step is to preprocess the data before using it for plotting. It will include :
Removing superfluous columns like ‘FIPS’, ‘Admin2', ‘Last_Update’ (since all the data is for single-day — 25th May).
Removing columns ‘Province_State’ and ‘Combined_Key’ since statewide data is not available for all the countries.
Grouping together data by ‘Country_Region’ and rename the column to ‘Country’
df.drop(['FIPS', 'Admin2','Last_Update','Province_State', 'Combined_Key'], axis=1, inplace=True)df.rename(columns={'Country_Region': "Country"}, inplace=True)df.head()
The data can be grouped together by the ‘groupby’ function of the dataframe. It is similar to the GROUPBY statement in SQL.
world = df.groupby("Country")['Confirmed','Active','Recovered','Deaths'].sum().reset_index()world.head()
Finally our data is cleaned and ready to use.
### Find top 20 countries with maximum number of confirmed casestop_20 = world.sort_values(by=['Confirmed'], ascending=False).head(20)### Generate a Barplotplt.figure(figsize=(12,10))plot = sns.barplot(top_20['Confirmed'], top_20['Country'])for i,(value,name) in enumerate(zip(top_20['Confirmed'],top_20['Country'])):    plot.text(value,i-0.05,f'{value:,.0f}',size=10)plt.show()
top_5 = world.sort_values(by=['Confirmed'], ascending=False).head()### Generate a Barplotplt.figure(figsize=(15,5))confirmed = sns.barplot(top_5['Confirmed'], top_5['Country'], color = 'red', label='Confirmed')recovered = sns.barplot(top_5['Recovered'], top_5['Country'], color = 'green', label='Recovered')### Add Texts for Barplotsfor i,(value,name) in enumerate(zip(top_5['Confirmed'],top_5['Country'])):    confirmed.text(value,i-0.05,f'{value:,.0f}',size=9)for i,(value,name) in enumerate(zip(top_5['Recovered'],top_5['Country'])):    recovered.text(value,i-0.05,f'{value:,.0f}',size=9)plt.legend(loc=4)plt.show()
A choropleth map is a type of thematic map in which areas are shaded or patterned in proportion to a statistical variable that represents an aggregate summary of a geographic characteristic within each area, such as population density or per-capita income.
Choropleth maps provide an easy way to visualize how a measurement varies across a geographic area or show the level of variability within a region
figure = px.choropleth(world,locations=’Country’, locationmode=’country names’, color=’Confirmed’, hover_name=’Country’, color_continuous_scale=’tealgrn’, range_color=[1,1000000],title=’Countries with Confirmed cases’)figure.show()
We can zoom into the map and hover over a particular region to see the confirmed number of cases for that country
Complete code is available at my GitHub repo: https://github.com/jaskeeratbhatia/covid-19-data-visulaization/blob/master/covid-19-data-25-may-2020-revised.ipynb
John Hopkins Github Repo for data: https://github.com/CSSEGISandData/COVID-19Wikipedia: https://en.wikipedia.org/wiki/Choropleth_map
John Hopkins Github Repo for data: https://github.com/CSSEGISandData/COVID-19