Period 6 · Grouped Summaries and Relationships
[video] Lecture video — Period 6: Grouped Summaries and Relationships — /api/lesson-media/videos/4d3653ff-a3ca-4f63-bf8e-bb93c553e599-1794847906148-e9ffc93a874fa9f0.mp4
[markdown] content available only on the platform
[markdown] content available only on the platform
Flashcards · Grouped Summaries and Relationships
df.groupby() — Splits rows into groups by one or more columns so a calculation can run on each group.
`df.groupby("region")["quantity"].sum()`
dropna=False (groupby) — Keeps a separate group for missing labels instead of silently dropping those rows.
`df.groupby("region", dropna=False)`
Named aggregation — An `agg()` call where each output column is named and given a (column, function) pair.
`units=("quantity", "sum")`
size vs count — `size` counts all rows in a group; `count` counts only non-missing values of a column.
`transactions=("transaction_id", "size")`
pivot_table() — Builds a grid summary with one variable as rows and another as columns; default aggregation is mean.
`df.pivot_table(index="region", columns="product_category", values="quantity", aggfunc="sum")`
Reconciliation check — Confirming that grouped totals add up to the total of the detailed data.
`assert by_region.sum() == df["quantity"].sum()`
Pearson correlation (r) — A number from -1 to 1 measuring the strength and direction of a linear relationship.
`df["quantity"].corr(df["unit_price"])` gives about -0.896.
Weighted average price — An average price that weights each transaction by quantity, unlike a simple mean across transactions.
Total revenue divided by total units sold.
Correlation is not causation — An association between two variables does not show that one causes the other.
Category may explain why price and quantity move together.
[pdf] Lecture slides — Period 6 (PDF) — /api/lesson-media/pdfs/4d3653ff-a3ca-4f63-bf8e-bb93c553e599-1794873712875-7d10df1d9fb65d7a.pdf
