Boolean masks
What it is
A comparison over a column produces a boolean Series; passing it to df[...] keeps the True rows.
Why it matters
Masks are vectorised and composable, which is what makes filtering fast and readable rather than a loop with an if.
In the real world: Keeping only the orders that were delivered, to report revenue that was actually earned.
Visual example
FilteringA boolean mask marks each row True or False; indexing with it keeps only the True rows.
Why this matters
Every filter in Pandas is this: build a True/False Series, then index with it. Combine masks with & and |, each in brackets.
Common misconception
Wrong idea: filtering renumbers the rows 0, 1, 2…
Correct idea: survivors keep their labels (0 and 2 here). Use .iloc or reset_index(drop=True) when you need positions.
- 1Build the mask
df["status"] == "completed"gives one True/False per row. - 2Index with it
df[mask]keeps the True rows only. - 3Labels come alongRows keep their original index labels.
mask = df["status"] == "completed"
mask.sum() # how many rows will survive — check before you filter
df[mask]