Selection & Filtering
15 min
Filtering a DataFrame uses the same boolean-masking idea as NumPy: compare a column to get a True/False Series, then index the DataFrame with it.
import pandas as pd
df = pd.DataFrame({
"name": ["Ada", "Grace", "Alan"],
"score": [92, 65, 78],
})
passing = df[df["score"] >= 70]
print(passing)
# name score
# 0 Ada 92
# 2 Alan 78
Chaining a column selection after a row filter is one of the most common pandas patterns — filter rows, then pull out just the column you need.
Two more selection tools you'll use constantly:
df.loc[label]— select by index label (row or column name).df.iloc[position]— select by integer position, like a list.
df.loc[0, "name"] # the 'name' value in the row labeled 0
df.iloc[0, 0] # the value in the first row, first column — by position
df.loc[df["dept"] == "eng", "name"] # filter rows AND pick one column, in one call
Combine multiple conditions with & (and) / | (or) — not Python's
and/or, which don't work element-wise on a Series — and wrap each
condition in parentheses: df[(df["a"] > 1) & (df["b"] < 5)].
Write `rows_where(data, col, threshold)`: build a DataFrame from `data`, and return the rows where `col` is greater than `threshold`, as a list of dicts (`.to_dict('records')`).
Write `select_columns(data, cols)`: build a DataFrame from `data`, and return only the columns named in `cols`, as `.to_dict('list')`.
What does df[df['score'] > 60] return?
You can filter DataFrame rows with a boolean mask and select specific columns, including chaining both together.