Orders carry only a customer_id. To report by customer tier you need the customer master joined in — and orders from unknown customers must not be silently dropped.
Write a function enrich(orders, customers) that left-joins customers onto orders on customer_id, and fills a missing tier with "Unknown".
This fixture runs before your code on every Run and Submit, so the data is always clean.
import pandas as pd
orders = pd.DataFrame(
{
"order_id": [1, 2, 3, 4, 5],
"customer_id": [101, 102, 103, 101, 999],
"amount": [500, 1200, 300, 750, 640],
}
)
customers = pd.DataFrame(
{
"customer_id": [101, 102, 103, 104],
"tier": ["Gold", "Silver", "Gold", "Bronze"],
}
)- Step 1: `orders.merge(customers, on="customer_id", how="left")` keeps every order row. An inner join — the pandas default — would quietly delete the order from customer 999.
- Step 2: Unmatched rows get `NaN` in the columns coming from the right frame, so `tier` is null for order 5.
- Step 3: `fillna("Unknown")` replaces those nulls with an explicit label, which is far easier to spot on a dashboard than a blank.
- Step 4: Customer 104 has no orders and correctly does not appear — a left join only keeps right-side rows that match.
import pandas as pd
def enrich(orders, customers):
merged = orders.merge(customers, on="customer_id", how="left")
merged["tier"] = merged["tier"].fillna("Unknown")
return mergedLearn the concept first
Stuck on Pandas — Joins? These free Python lessons teach it step by step.
More Pandas — Joins questions
Practising alone only goes so far — our live cohorts pair Python and Pandas depth with dashboards, agentic analytics projects and mock interviews.
Explore the program