On this page
Session 3 · Combine and summarize data
Exam focus. Select the join by defining which population must survive, validate keys, and summarize at the right grain.
What to know
A left join keeps every left-hand row. An inner join retains matched keys only. A right join preserves the right-hand population, and an outer join preserves records from both sides. Check key uniqueness to avoid duplicating amounts.
Code pattern
audit = purchases.merge(suppliers, on="Supplier_ID", how="left")
unmatched = audit.loc[audit["Country"].isna()]
print(len(unmatched), unmatched["Amount_EUR"].sum())
summary = audit.groupby("Country")["Amount_EUR"].agg(["sum", "count"])
Exam traps
A default groupby drops missing group keys. Audit those rows explicitly if they matter to the total. concat stacks rows by default, and ignore_index=True only resets row labels. df.T swaps axes. Prefer vectorized arithmetic when it expresses the calculation clearly.
Write an interpretation
State the result, explain what produced it, and say why it matters for the decision in the case.
Source: Data Handling Final Revision File and the exam announcement and mock supplied in chat.
Exam methods and worked application
| Tool | Effect | Typical use |
|---|---|---|
| Inner join | Keeps matching keys | Analyze matched records only |
| Left join | Keeps all rows of the left dataset | Audit supplier coverage without losing purchases |
| Right join | Keeps all rows of the right dataset | Preserve the reference population on the right |
| Outer join | Keeps matched and unmatched records from both | Reconcile two data sources |
concat | Stacks compatible files along an axis | Combine monthly extracts before checking duplicates |
df.T | Swaps rows and columns | Reorient a small table without aggregating |
Worked audit and group summary
audit = purchases.merge(
suppliers, on="Supplier_ID", how="left",
validate="many_to_one", indicator=True,
)
unmatched = audit.loc[audit["_merge"] == "left_only"]
print(len(unmatched), unmatched["Amount_EUR"].sum())
summary = audit.groupby("Country", dropna=False).agg(
Total_Spent=("Amount_EUR", "sum"),
Purchase_Count=("Purchase_ID", "size"),
Average_Spent=("Amount_EUR", "mean"),
).reset_index()
If three purchases are €20, €35 and €45 and the €35 supplier has no match, the left join preserves €100 and flags €35 for investigation. The inner join reports €65 from matched purchases. Explicit match status also distinguishes an unmatched supplier from one whose country was missing in the source. Use size for rows and count for nonmissing values. Before aggregating, check the row count and key uniqueness. Otherwise duplicate keys can multiply purchases. Vectorized subtraction and ratio calculations operate on whole columns. Loops remain useful when iterating through files or implementing genuinely sequential logic.
Official guide · Loops, vectorization and debugging
Practice joins and aggregation, then debug both syntax and logic. When joining, ask which rows must survive and whether the lookup key is unique. Match the aggregation to the business question. Check that filling missing data actually assigns the result and that the output includes the intended rows. Use vectorized operations for whole-column calculations, and practice a short loop for conditional row transformations.
Official Exam Preparation Guidelines (pp. 1–3).