Analyzing Coronavirus (Covid-19) Data Using Pandas and Plotly
By: Mohammad Ashhad
Tags: Pandas, Co Vid 19, Plotly, Python, Coronavirus

Analyzing Coronavirus (Covid-19) Data Using Pandas and Plotly

Photo by CDC on Unsplash

Covid-19 that started out in China has now spread globally with countries scrambling to tackle it. The virus that started out as a healthcare emergency has now started to have serious economic consequences. In this article, I have tried to understand and visualize the spread of Covid-19 using Pandas and Plotly.

We start with importing all the libraries and loading all the data. The data is hosted by John Hopkins University and can be found in the link here. There are three files that contain Total Confirmed Cases, Deaths and Recoveries.

After running the code above, this is how the data looks like.

We have

Dates (as columns)

Total Confirmed Cases

Province

Countries

Latitude and Longitude

Even though the data is nicely organized, we will modify it a bit to make it more suitable for plotting. We can use the pandas melt function to reshape the dates in one column so that we can set it as index later, which makes it much easier for plotting.

With dates and Countries as indices, the datsets are now cleaner and much easier to plot. See the snapshot below.

For the purpose of this article, we will only be looking at the data at a country level and not at the Province/State level. Let’s create a consolidated dataset that combines the datasets for Cases, Deaths and Recoveries. I have also created a function to get daily count from the cumulative data. We use the following code for the processing.

I have also created a few calculated fields like Active Cases (Cases without the outcome of either deaths or recovery) and Deaths to Cases Ratio and combined all datasets using the merge function from pandas.

Active Cases = Confirmed Cases - Recoveries - Deaths

The Consolidated dataset has the index as country and date and each of the metric as a column (See the snapshot below)

We use plotly.graph_objects for all of our plotting. It is a neat solution to make beautiful graphs without a lot of code. We use the code below to create five charts indicating global numbers for Confirmed Cases, Active Cases, Deaths, Recoveries, Deaths to Cases Ratio.

Key Metrics for WorldWide Covid19 Cases

We can see that the Confirmed cases, Deaths and Recoveries are growing, but it is hard to draw the extent of growth from these numbers since these are cumulative. The ratio of Death to Cases ratio is 3.4% (This is what was reported by the WHO a few days ago).

The utility of ‘Active Cases’ as a metric is immediately seen since it provides a snapshot of the outbreak at a given point in time. From the chart, we can see that the Active Cases have been declining since mid February which is a good indicator that we are getting a better hold on the outbreak. This chart alone however is misleading since we know that most of the cases are in China and that could be skewing the numbers. Let’s compare the Active Cases for the region excluding China.

The plot above paints two very different pictures for the situation inside and outside China. The outbreak appears to be under control with the number of cases declining inside China but it is still growing quite strongly outside China.

Let’s dig a little further into the data for each country. To make sense of the data, let’s limit ourselves to top 10 countries where the cases are all greater than 300. Let’s plot these countries by the number of cases using the code below:

Data as of 5th March 2020

Now let’s see how the cases grew over time for the top four countries. Using the code below, we count the number of cases from the ‘start of the outbreak’ which is defined as the day when the first case is reported

As an example, the output for Italy below, shows the first date as ‘2020–01–31’ as the beginning of the outbreak with two confirmed cases. The index could then be interpreted as ‘Day 0’.

Let’s now plot the data for the top four countries by the number of cases and see how the outbreak grew over time using the code below:

Let’s look at the output below. Notice, how fast the numbers grow once the growth starts.

The outbreaks in each of these countries started gradually but grew exponentially once it reached ‘outbreak’ threshold. The charts above are a warning against complacency. If not contained, the cases can compound very fast and the growth is almost exponential so even a small number of cases could balloon into a full blown outbreak very fast.

It took 29 days to cross 100 cases in South Korea and just 16 more days to cross 7000! In Italy, it took 23 days to cross 100 and just 13 more days to cross 5000!

Lastly, since the situation varies greatly by country, hence it is worthwhile to create a view where we can deepdive on a specific country. Here, we can use the ipywidgets library to make our chart interactive and make the country selection possible from a dropdown using the code below:

The output below is very similar to the one above but we now have a dropdown from which we can select the country (chart shown for Italy).

Again, we notice that the growth is sudden and quite rapid and despite the high number of cases, Italy still has not peaked in terms of the total number of cases!

The number of cases and deaths worldwide have been growing but we can see in the data from China that with proper measures the outbreak can be brought under control. If you want to analyze the data on your own using my code, please find the link to the jupyter notebook here.

Let’s hope and pray for a speedy end to the outbreak!