Tracking Covid-19 with Pandas and Plotly Open Source Graphing Library Maps.

JOSÉ MANUEL NÁPOLES DUARTE
3 min readMar 29, 2020

--

Photo by Markus Spiske

Mexico is still in the second phase of the spread of the SARS-Cov-2 virus and has had a very valuable opportunity when observing what has happened in other countries where today the situation is critical.
In this exercise, I will show how the cases reported by 1) The Center for Systems Science and Engineering (CSSE) at Johns Hopkins University for cases from around the world and 2) from the information provided by the Mexican government for cases reported as positive to the Covid-19 in the Mexican Republic, can be tracked using Pandas and the Plotly Express library to generate interactive maps.

First we invoke the required libraries:

Johns Hopkins University Center for Systems Science and Engineering (JHU CSSE) team has put on github the data repository for the 2019 Novel Coronavirus Visual Dashboard. Here I am going to focus on the data corresponding to confirmed cases for all countries since 1/22/20:

We can get the csv file with pandas function pd.read_csv:

In the figure below we can see the structure of this file:

We can see that the names of the countries are given as well as the geographical coordinates of latitude (Lat) and longitude (Long) and the number of positive cases per day.

  1. The next thing will be to generate the map with Plotly Express (see https://plotly.com/python/maps/).

In the code above, the "Lat" and "Lon" arguments correspond to the geometric coordinates. We associate the size of the marks with the number of cases on a particular day, in this case it is taken from the column of the day "3/28/20". Also the color of the marks is obtained by dividing the values ​​in that column by 1000 (arbitrarily). Finally the projection argument is established as "natural earth" to have the representation of the globe.

2. Next, we can apply these tools to visualize the cases that are occurring in Mexico using the official report of the Mexican government: https://www.gob.mx/salud/documentos/coronavirus-covid-19-comunicado-tecnico-diario-238449

To make this data manageable, I am going to manipulate with Pandas using DataFrames. For this I generated an additional file (“df3”) with the names of the states of Mexico and their coordinates in addition to the file (“df2”) with the information of the occurrences of confirmed cases:

The following part of the code counts the cases in each state:

To make the map we must build a DataFrame with the data set appropriately, here I will use the columns "State", "Lat" and "Long" for "df3" and I will add a column with the "conteo" list obtained in the previous cycle:

And we are ready to represent the cases in Mexico:

You can see the interactive map from the following link: https://plotly.com/~napolesd/93/

The code shown above can be opened using google colaboratory in the link to github https://gist.github.com/napoles-uach/c897360aef55a46bd078faf53b79f5a9

--

--

JOSÉ MANUEL NÁPOLES DUARTE
JOSÉ MANUEL NÁPOLES DUARTE

Written by JOSÉ MANUEL NÁPOLES DUARTE

Physics/ML Lecturer at FCQ-UACH. Member of the Streamlit Creators program.

No responses yet