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.
.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.
products and revenues are same-length lists (possibly empty); revenue values are ints.
total_revenue(["Widget", "Gadget"], [1200, 950]) → 2150total_revenue([], []) → 0
Show Solution
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.
products and revenues are same-length lists.
filter_high_revenue(["Widget", "Gadget", "Gizmo"], [1200, 950, 1830], 1000) → ["Widget", "Gizmo"]
Show Solution
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).
products may repeat; revenues is the same length.
average_revenue_per_product(["Widget", "Gadget", "Widget"], [1000, 500, 2000]) → {"Widget": 1500.0, "Gadget": 500.0}
Show Solution
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).
regions may repeat; revenues is the same length.
total_by_region(["East", "West", "East"], [1200, 950, 1830]) → {"East": 3030, "West": 950}
Show Solution
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.
n is a positive int, no greater than the number of products.
top_n_products(["Widget", "Gadget", "Gizmo"], [1200, 950, 1830], 2) → ["Gizmo", "Widget"]
Show Solution
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.
revenues is a list of numbers and/or None.
count_missing_revenue([1200, None, 950, None]) → 2count_missing_revenue([1, 2, 3]) → 0
Show Solution
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.
revenues is a list of numbers and/or None.
fill_missing_with_zero([1200, None, 950]) → [1200.0, 0.0, 950.0]
Show Solution
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).
position is a valid row position.
row_at_position(["Widget", "Gadget"], [1200, 950], 1) → {"product": "Gadget", "revenue": 950}
Show Solution
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"])}