Skip to content

Using Pandas for COVID-19 Data Analysis

Updated: at 01:14 AM

The COVID-19 pandemic has been an immense global health crisis, affecting millions of lives around the world. The large volumes of data generated during this time present an opportunity to leverage data analysis to derive valuable insights and inform critical decision making. Python’s Pandas library is a powerful tool for data manipulation and analysis, making it well-suited for examining COVID-19 data. In this comprehensive tutorial, we will walk through the process of using Pandas for COVID-19 data analysis, from importing and cleaning data to analyzing and visualizing trends.

Table of Contents

Open Table of Contents


Pandas is an open-source Python library that provides high-performance, easy-to-use data structures and data analysis tools. It is built on top of the NumPy package and allows for fast analysis and data cleaning and preparation. Pandas includes two main data structures - DataFrames and Series - which make importing, analyzing, and visualizing data intuitive and efficient.

Some key features of Pandas include:

For COVID-19 data, Pandas provides the capabilities to efficiently import disparate datasets, combine them into structured DataFrames, clean erroneous or missing data, and uncover insights through statistical analysis and compelling visualizations. The steps outlined in this guide will demonstrate how to harness Pandas for unlocking value from COVID-19 data.

# Import pandas
import pandas as pd

Loading Datasets into Pandas

The first step is importing COVID-19 datasets into Pandas DataFrames. Pandas can load data from a variety of sources and formats such as CSV, Excel, SQL databases, JSON, and HTML tables. For this example, we will use the CSV file format, which is common for sharing public datasets.

We can use Pandas’ read_csv() method to load CSV data into a DataFrame by passing the file path or URL.

covid_df = pd.read_csv('covid_data.csv')

By default, read_csv() will infer the column names and data types. We can explicitly set these parameters for more control over the import process.

covid_df = pd.read_csv('covid_data.csv',
                      dtype={'date': 'object'},

Here we set the ‘date’ column to datetime datatype and specify the remaining columns should be imported as object datatype.

For large datasets, we can also handle the data in chunks by setting the chunksize parameter. This loads a subset of rows at a time rather than loading the entire file into memory.

covid_df = pd.read_csv('large_covid_data.csv', chunksize=1000)

In addition to CSV, Pandas supports various other data formats:

These allow loading data from diverse sources into Pandas DataFrames for further analysis.

Inspecting and Cleaning Data

Once data is loaded, it is often necessary to inspect, clean, and preprocess it before analysis.

Pandas provides a suite of methods for data preparation tasks like:

Handling Missing Data

It is common for real-world datasets to have missing values encoded as blanks, NaN, or other placeholders. Pandas provides utilities for detecting, removing, and replacing missing data.

Detect missing values:


Drop rows with missing values:


Fill in missing values:


Data Validation

We may need to validate or clean data that is in incorrect formats or contains anomalies.

Identify validation issues:

covid_df.dtype == 'object'

Convert data types:

covid_df['date'] = pd.to_datetime(covid_df['date'])

Filter out anomalies:

normal_data = covid_df[covid_df['cases'] < 1000]

Handling Duplicates

Pandas makes it easy to find and remove duplicate rows:



Renaming Columns

The column names can be changed using rename():

covid_df = covid_df.rename(columns={'old_name': 'new_name'})

Adding/Deleting Columns

New columns can be added by simply assigning the values:

covid_df['vaccination_pct'] = covid_df['vaccinations'] / covid_df['population']

And columns deleted using drop():

covid_df = covid_df.drop(columns=['unneeded_col'])

By leveraging these data preparation capabilities, we can ensure our DataFrames are ready for analysis and visualization.

Data Analysis with Pandas

Once our COVID-19 data is imported and cleaned, Pandas provides a variety of options for analysis and insights generation.

Summary Statistics

We can generate high-level summary statistics on DataFrame columns using describe():


This outputs counts, means, quartiles, and other aggregates - a quick way to understand distributions.

For individual columns, we can generate specific aggregates like mean, median, max:



Pandas groupby() method allows splitting DataFrames by categories and computing aggregates on those groups.

For example, calculate cases per country:


Multiple aggregates on groups:

covid_df.groupby('date')['cases'].agg([min, max, mean])

Time Series Analysis

Pandas has built-in time series capabilities, which lend themselves well to analyzing temporal COVID-19 data.

Parse datetime data:

covid_df['date'] = pd.to_datetime(covid_df['date'])

Rolling statistics:


Time resampling:




These enable analyzing COVID trends over time at various granularities.

Merging/Joining Datasets

To combine COVID-19 data from different sources, Pandas provides efficient merge() and join() operations similar to SQL:

merged_df = pd.merge(df1, df2, how='inner', on='date')

This can create unified datasets for broader insights.

Correlation Analysis

Finding correlations between COVID-19 indicators can uncover key relationships:


Plots like scatterplots can also visualize correlations.

These Pandas techniques enable cutting through COVID-19 complexity to derive actionable insights.

Data Visualization with Pandas

Pandas integrates tightly with Matplotlib to enable data visualization directly from DataFrames. We can create various plots and charts to present COVID-19 trends and insights.

Line Plots

Great for visualizing timeseries data:


Bar Plots

Useful for comparing categorical data:



Examining case distributions:

covid_df['cases'].plot(kind='hist', bins=100)

Scatter Plots

Visualize correlations between variables:

covid_df.plot(kind='scatter', x='cases', y='deaths')

Customizing Plots

All plots can be customized via Matplotlib:

import matplotlib.pyplot as plt

ax = covid_df.plot(kind='bar', figsize=(12, 8))
ax.set_ylabel('Total Cases')
ax.set_title('COVID-19 Cases per Country')

This enables creating publication-quality plots to convey findings.

By leveraging Pandas tight integration with Matplotlib and Seaborn, we can build visualizations that intuitively communicate COVID-19 insights.

Example: COVID-19 Data Analysis Workflow

Putting together the concepts covered, here is an end-to-end example workflow for COVID-19 data analysis with Pandas:

Load datasets:

cases_df = pd.read_csv('covid_cases.csv')
testing_df = pd.read_csv('covid_testing.csv')

Inspect and clean data:

cases_df['date'] = pd.to_datetime(cases_df['date'])
testing_df.fillna(0, inplace=True)

Join data:

covid_df = cases_df.merge(testing_df, on='date')


covid_df['positive_rate'] = covid_df['cases'] / covid_df['tests']



By chaining together Pandas capabilities like this, we can go from raw COVID-19 data to actionable insights quickly and efficiently.


In this comprehensive guide, we explored how to leverage the powerful Pandas Python library for COVID-19 data manipulation, analysis, and visualization. Pandas provides fast, flexible data structures in DataFrames along with a vast set of tools for cleaning, munging, analyzing, and visualizing data. By following the examples and techniques outlined here, Python developers, data analysts, scientists, and researchers can use Pandas to extract valuable insights from COVID-19 datasets. Pandas enables efficiently wrangling complex COVID-19 data, identifying trends and correlations, and communicating findings through stunning visuals. As the pandemic continues evolving, Pandas will remain an indispensable tool for unlocking the secrets hidden within the burgeoning array of COVID-19 data.