Chapter 5 · 6 levels
Filtering and sorting
Pick rows by position and label, filter with boolean masks, combine conditions, and sort with tie-breakers on the Harbor & Pine tables.
StartMost questions you’ll get as an analyst start with “only the…”: only marketplace orders, only completed orders from the app, only lighting under $200, only sessions that came from email. Then they end with “…the top ten”. Filtering and sorting are those two halves.
In pandas, filtering works through boolean masks: a column of True and False values, one per row, that says which rows to keep. Once that idea clicks, every filter looks the same, whether it has one condition or five.
This chapter also introduces .loc and .iloc, the two precise ways to select rows and columns
together, and sort_values with proper tie-breaking so your top-N lists are stable. The boss level
puts it all together on 40,000 website sessions.
Levels
- Rows by position, columns by name
from Thomas Whitaker, Head of Merchandising
loc, iloc, index, row selection
- Combine conditions with & and |
from Daniel Brooks, Head of Operations
boolean mask, and or not, isin, parentheses
- The ten most expensive products
from Thomas Whitaker, Head of Merchandising
sort values, ascending, tie breaking, head, reset index
- Lighting in a price band
from Jordan Reyes, Head of Marketing
between, str contains, filtering, sorting
- Boss: who converts on the website?
from Jordan Reyes, Head of Marketing
boolean mask, mean of boolean, sort values, loc