🐍 Python for Everyone

← Back to practice bank

Chapter 24 Coding Challenges 50-65 min, auto-graded

Coding Challenges: Pandas for Data Analysis

8 HackerRank-style challenges, auto-graded right in your browser using real pandas — no local install needed. Write your function in the code box, then click Run Tests to check it against a set of test cases (including edge cases). Stuck? Every challenge has a Show Solution reveal at the bottom. Only uses what Chapters 1-24 have covered so far. No import beyond pandas/numpy/json/os/re/math/datetime/random/csv/threading/concurrent.futures.

How grading works Your code runs entirely in your browser using Pyodide (Python compiled to WebAssembly), with pandas loaded automatically alongside it — nothing is uploaded anywhere. Keep the function name in the stub exactly as given, since the grader calls it by name. Return plain Python types (lists/dicts/numbers/strings) from your function — never a raw DataFrame or Series — using .tolist()/.to_dict()/int()/float() so the grader can compare your result reliably.

1 Total Revenue Easy

Write a function total_revenue(products, revenues) that builds a DataFrame from the two plain Python lists (columns "product" and "revenue") and returns the total of the revenue column as a plain Python int.

Constraints: products and revenues are same-length lists (possibly empty); revenue values are ints.
Examples:
total_revenue(["Widget", "Gadget"], [1200, 950]) → 2150
total_revenue([], []) → 0
challenge_1.py
Show Solution
solution_1.py
import pandas as pd

def total_revenue(products, revenues):
    df = pd.DataFrame({"product": products, "revenue": revenues})
    return int(df["revenue"].sum())

2 Filter High-Revenue Products Medium

Write a function filter_high_revenue(products, revenues, threshold) that builds a DataFrame, uses boolean filtering to keep only rows where revenue is strictly greater than threshold, and returns the matching product names as a plain Python list of strings, in their original order.

Constraints: products and revenues are same-length lists.
Examples:
filter_high_revenue(["Widget", "Gadget", "Gizmo"], [1200, 950, 1830], 1000) → ["Widget", "Gizmo"]
challenge_2.py
Show Solution
solution_2.py
import pandas as pd

def filter_high_revenue(products, revenues, threshold):
    df = pd.DataFrame({"product": products, "revenue": revenues})
    return df[df["revenue"] > threshold]["product"].tolist()

3 Average Revenue Per Product Medium

Write a function average_revenue_per_product(products, revenues) that builds a DataFrame, groups by product, and returns the average revenue per product as a plain Python dict mapping product name (str) to average revenue (float).

Constraints: products may repeat; revenues is the same length.
Examples:
average_revenue_per_product(["Widget", "Gadget", "Widget"], [1000, 500, 2000]) → {"Widget": 1500.0, "Gadget": 500.0}
challenge_3.py
Show Solution
solution_3.py
import pandas as pd

def average_revenue_per_product(products, revenues):
    df = pd.DataFrame({"product": products, "revenue": revenues})
    result = df.groupby("product")["revenue"].mean()
    return {k: float(v) for k, v in result.items()}

4 Total by Region Medium

Write a function total_by_region(regions, revenues) that builds a DataFrame, groups by region, and returns total revenue per region as a plain Python dict mapping region name (str) to total revenue (int).

Constraints: regions may repeat; revenues is the same length.
Examples:
total_by_region(["East", "West", "East"], [1200, 950, 1830]) → {"East": 3030, "West": 950}
challenge_4.py
Show Solution
solution_4.py
import pandas as pd

def total_by_region(regions, revenues):
    df = pd.DataFrame({"region": regions, "revenue": revenues})
    result = df.groupby("region")["revenue"].sum()
    return {k: int(v) for k, v in result.items()}

5 Top N Products Medium

Write a function top_n_products(products, revenues, n) that builds a DataFrame, sorts by revenue descending, and returns the top n product names as a plain Python list of strings, highest revenue first.

Constraints: n is a positive int, no greater than the number of products.
Examples:
top_n_products(["Widget", "Gadget", "Gizmo"], [1200, 950, 1830], 2) → ["Gizmo", "Widget"]
challenge_5.py
Show Solution
solution_5.py
import pandas as pd

def top_n_products(products, revenues, n):
    df = pd.DataFrame({"product": products, "revenue": revenues})
    return df.sort_values("revenue", ascending=False).head(n)["product"].tolist()

6 Count Missing Revenue Values Easy

Write a function count_missing_revenue(revenues) that builds a one-column DataFrame from revenues (a list that may contain None values) and returns how many values are missing, as a plain Python int.

Constraints: revenues is a list of numbers and/or None.
Examples:
count_missing_revenue([1200, None, 950, None]) → 2
count_missing_revenue([1, 2, 3]) → 0
challenge_6.py
Show Solution
solution_6.py
import pandas as pd

def count_missing_revenue(revenues):
    df = pd.DataFrame({"revenue": revenues})
    return int(df["revenue"].isna().sum())

7 Fill Missing Revenue With Zero Medium

Write a function fill_missing_with_zero(revenues) that builds a one-column DataFrame, fills any missing revenue value with 0, and returns the whole column as a plain Python list of floats.

Constraints: revenues is a list of numbers and/or None.
Examples:
fill_missing_with_zero([1200, None, 950]) → [1200.0, 0.0, 950.0]
challenge_7.py
Show Solution
solution_7.py
import pandas as pd

def fill_missing_with_zero(revenues):
    df = pd.DataFrame({"revenue": revenues})
    df["revenue"] = df["revenue"].fillna(0)
    return df["revenue"].tolist()

8 Row at a Position Easy

Write a function row_at_position(products, revenues, position) that builds a DataFrame and uses .iloc[] to return the row at integer position position as a plain Python dict with keys "product" (str) and "revenue" (int).

Constraints: position is a valid row position.
Examples:
row_at_position(["Widget", "Gadget"], [1200, 950], 1) → {"product": "Gadget", "revenue": 950}
challenge_8.py
Show Solution
solution_8.py
import pandas as pd

def row_at_position(products, revenues, position):
    df = pd.DataFrame({"product": products, "revenue": revenues})
    row = df.iloc[position]
    return {"product": row["product"], "revenue": int(row["revenue"])}
← Practice Bank Interview Questions →