Selecting and Filtering Rows
About 22 minutes
Why this matters
Most questions about data start with “just the rows where…”. Which customers are adults? Which orders are from Seattle? pandas answers with boolean masks, the same idea you met in NumPy, applied to a table.
The idea
import pandas as pd
people = pd.DataFrame({
"name": ["Ada", "Bo", "Cy", "Di"],
"age": [34, 17, 45, 29],
"city": ["Rome", "Oslo", "Rome", "Oslo"],
})
mask = people["age"] >= 18 # True/False for each row
people[mask] # only the adults
Put the mask straight inside the square brackets: people[people["age"] >= 18].
Combine conditions with & (and), | (or) and ~ (not). Unlike plain Python, each condition must be in parentheses, and you cannot use the words and / or:
people[(people["age"] >= 18) & (people["city"] == "Rome")]
More tools:
people[["name", "city"]] # choose columns (note the double brackets)
people[people["city"].isin(["Rome"])] # value is one of a list
people.sort_values("age", ascending=False)
people.sort_values("age").reset_index(drop=True) # renumber the rows from 0
people.loc[people["age"] > 30, "name"] # rows by mask, one column
A filtered table keeps its old row numbers, which is why reset_index(drop=True) is useful when you want a clean 0, 1, 2, … again.
Try it
Build the table above. Print the people in Oslo who are over 20. Then print just their names as a list with .tolist().
Your turn
The functions receive a table with the columns name, age and city. Write:
adults(df): only the rows whereageis 18 or more.names_in_city(df, city): a list of the names of the adults in that city.select_columns(df, cols): a table with only the named columns.in_cities(df, cities): the rows whose city is in the listcities.oldest_first(df): all rows, oldest first, with row numbers starting again from 0.
Recap
df[mask]keeps the rows where the mask isTrue.- Join conditions with
&,|,~, each in parentheses. df[["a", "b"]]picks columns;isintests membership;sort_valuesorders rows.reset_index(drop=True)renumbers rows after filtering or sorting.
Common mistakes
- Using
and/orbetween masks. That raises an error; use&/|. - Forgetting the parentheses:
df["a"] > 1 & df["b"] < 5does the&first and fails. - Single brackets for several columns. Use a list inside the brackets:
df[["name", "city"]].