Thuta Learning
ExercisesData & Databasesintermediate

Exercises: Applied Data Cleaning & Analysis

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Practice the material from Applied Data Cleaning & Analysis hands-on
  • Reinforce the skills you've already learned through practice
  • Get comfortable finding bugs, fixing them, and checking your own work

Quick Think

This lesson steps up a level from lesson 1 — the goal is to combine skills like handling multiple conditions, cleaning real-world data with missing values, calculating new columns, grouping and summarizing, and merging two tables into a single data flow. This isn't new teaching content — you'll simply reapply the concepts explained in the conditional-selection, handling-missing, operations, groupby, and merging lessons. Since each task builds on the one before it, we recommend working through them in order.

Exercises

Task 1: In a DataFrame with Product, Category, Price, and Qty columns, set some of the Price values to NaN, check the missing count with isna().sum(), then fill them in with fillna() using the column mean. Task 2: Filter rows where Category == "Drink" and Qty > 2 using conditional selection with the & operator. Task 3: Create a new Total column (Price * Qty), then use groupby("Category") to calculate the Total sum for each category. Task 4: Build a new table with Category and Supplier name, then use merge() to combine it with the original DataFrame based on the Category column (try using how="left").

Code Example

python
import pandas as pd
import numpy as np

data = {
    "Product": ["Tea", "Coffee", "Juice", "Cake", "Water"],
    "Category": ["Drink", "Drink", "Drink", "Food", "Drink"],
    "Price": [1200, 1800, np.nan, 2500, 700],
    "Qty": [3, 2, 5, 1, 4]
}
df = pd.DataFrame(data)

# Task 1: missing data
# TODO: df["Price"].isna().sum()
# TODO: df["Price"] = df["Price"].fillna(df["Price"].mean())


# Task 2: multi-condition filter
# TODO: df[(df["Category"] == "Drink") & (df["Qty"] > 2)]


# Task 3: new column + groupby
# TODO: df["Total"] = df["Price"] * df["Qty"]
# TODO: df.groupby("Category")["Total"].sum()


# Task 4: merge
suppliers = pd.DataFrame({
    "Category": ["Drink", "Food"],
    "Supplier": ["ABC Beverage", "XYZ Bakery"]
})
# TODO: pd.merge(df, suppliers, on="Category", how="left")
You should see
You'll end up with a clean DataFrame with no missing values, a filtered set of Drink rows, a table of Total sums by Category, and a merged table that includes the Supplier name.

5-Minute Try

In 5 minutes, run Task 2 and Task 3 back to back, then work out by hand what the Total sum for the Drink category should be in the groupby result and check it against the actual output.

A Quick Warning

Before filling in the mean with fillna(), check whether the column's data type is actually numeric — trying to fill a text column with a mean value will throw an error.

Easy traps

  • People often write two conditions with the and/or keywords instead of the & / | operators, and forget the parentheses, which triggers an error
  • When using merge(), if the values in the on column don't match in spelling or case ("Drink" vs "drink"), the rows won't match and you'll end up with NaN

Try It Yourself Now

In 5 minutes, run Task 2 and Task 3 back to back, then work out by hand what the Total sum for the Drink category should be in the groupby result and check it against the actual output.

You'll know it worked when: You'll end up with a clean DataFrame with no missing values, a filtered set of Drink rows, a table of Total sums by Category, and a merged table that includes the Supplier name.

Exercises: Applied Data Cleaning & Analysis | Thuta Learning