Python Central HubSign in

Data Analytics with Python / Phase 10: Data Analytics Projects

Covid-19 Data Analysis & Visualization

Analyze time series Covid data - trends, moving averages, comparisons, and clear dashboards/plots.

Goal

Build a small Covid-19 analysis project:

  • Load time series case data
  • Clean/parse dates
  • Plot trends and moving averages
  • Compare regions

Analysis pipeline

diagram Covid-19 trend analysis pipeline mermaid
From noisy daily case counts to a clear, comparable trend.

Data sources

Possible sources:

  • Our World in Data (OWID)
  • Johns Hopkins dataset

Step 1: Load and inspect

Python
import pandas as pd
 
df = pd.read_csv("data/covid.csv")
print(df.shape)
print(df.head())

Step 2: Parse dates and subset

Python
import pandas as pd
 
if "date" in df.columns:
    df["date"] = pd.to_datetime(df["date"], errors="coerce")

Step 3: Trend plot

Python
import matplotlib.pyplot as plt
 
country = "India"
sub = df[df["location"] == country].sort_values("date")
 
plt.figure(figsize=(10, 4))
plt.plot(sub["date"], sub["new_cases"], label="new_cases")
plt.title(f"New cases over time - {country}")
plt.xlabel("Date")
plt.ylabel("New cases")
plt.xticks(rotation=20)
plt.tight_layout()
plt.show()

Step 4: Moving average

Python
sub = sub.copy()
sub["new_cases_ma7"] = sub["new_cases"].rolling(7).mean()
 
plt.figure(figsize=(10, 4))
plt.plot(sub["date"], sub["new_cases"], alpha=0.4, label="daily")
plt.plot(sub["date"], sub["new_cases_ma7"], label="7-day avg")
plt.title(f"New cases (7-day avg) - {country}")
plt.legend()
plt.tight_layout()
plt.show()

Step 5: Compare regions

Python
import seaborn as sns
import matplotlib.pyplot as plt
 
countries = ["India", "United States", "Brazil"]
sub = df[df["location"].isin(countries)].copy()
sub = sub.sort_values("date")
 
plt.figure(figsize=(10, 4))
sns.lineplot(data=sub, x="date", y="new_cases", hue="location")
plt.title("New cases comparison")
plt.xticks(rotation=20)
plt.tight_layout()
plt.show()

Visualize it

sketch Daily cases vs 7-day average p5.js
The faint line is noisy daily data; the bold amber line is the smoothed trend.

Deliverable

Write a short report:

  • When did peaks occur?
  • Is the trend seasonal?
  • What data quality issues exist (missing dates/values)?

Check your understanding

quizCheck yourself
  1. Why does the project parse the date column with pd.to_datetime(df["date"], errors="coerce")?

    Show answer

    A โ€” It turns text into real datetimes and sets unparseable values to NaT instead of crashing โ€” Real datetimes are needed for sorting and time plots, and coerce flags bad values as NaT rather than raising an error.

  2. Why add new_cases_ma7 = new_cases.rolling(7).mean()?

    Show answer

    A โ€” Daily counts are noisy, and a 7-day window smooths them so the trend shows โ€” A single day tells you little, and a weekly window also evens out reporting cycles.

  3. Before computing the rolling average for India, what does the project do to the subset?

    Show answer

    A โ€” Sorts it by date โ€” Rolling windows assume ordered rows, so the subset is sorted by date first.

  4. Which data quality issue does the deliverable ask you to look for?

    Show answer

    A โ€” Missing dates or values โ€” Gaps in dates or values distort trends and moving averages.

๐Ÿงช Try It Yourself

Exercise 1 โ€“ Parse dates

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 โ€“ 7-day rolling average

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 โ€“ Filter one country's data

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did