Setup
A boolean filter works in two steps: first you build a mask — a Series of True/False,
one per row — then you use that mask to keep only the True rows.
import pandas as pd
df = pd.DataFrame({
"name": ["Asha", "Ravi", "Meera", "Zoya", "Kabir"],
"age": [23, 28, 26, 31, 22],
"city": ["Pune", "Delhi", "Delhi", "Pune", "Mumbai"],
"score": [88, 91, 77, 95, 60],
})
print(df)Basic boolean filtering
print(df[df["age"] >= 25])Multiple conditions: AND / OR
Pandas uses bitwise operators:
- AND:
& - OR:
|
Always use parentheses:
mask = (df["city"] == "Delhi") & (df["score"] >= 80)
print(df[mask])mask = (df["city"] == "Mumbai") | (df["score"] >= 90)
print(df[mask])isin() for membership checks
mask = df["city"].isin(["Pune", "Delhi"])
print(df[mask])between() for range filters
mask = df["age"].between(23, 30, inclusive="both")
print(df[mask])query() for readable filters
query() can be easier to read, especially in notebooks.
result = df.query('city == "Delhi" and score >= 80')
print(result)Variables inside query
min_score = 85
result = df.query('score >= @min_score')
print(result)Common mistakes
- Using
and/orinstead of&/|(will error) - Forgetting parentheses around conditions
- Comparing strings with extra spaces (clean using
.str.strip())
Visualize it
flowchart LR A["df['score'] >= 80"] --> B["Boolean mask (True/False per row)"] B --> C["df[mask]"] C --> D["Only matching rows"]
🧪 Try It Yourself
Exercise 1 – Build a Boolean Mask
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Combine Conditions With &
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Membership With isin
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Check your understanding
Which operators combine conditions in a pandas boolean filter?
Pandas uses the bitwise operators & and |, with parentheses around each condition. Using and or or raises an error.
Show answer
B — & and | — Pandas uses the bitwise operators & and |, with parentheses around each condition. Using and or or raises an error.
In the sample data, which rows match (df["city"] == "Delhi") & (df["score"] >= 80)?
Ravi and Meera live in Delhi, but Meera scored 77, so only Ravi (91) passes both tests.
Show answer
A — Ravi only — Ravi and Meera live in Delhi, but Meera scored 77, so only Ravi (91) passes both tests.
How many rows does df["age"].between(23, 30, inclusive="both") select for ages 23, 28, 26, 31 and 22?
23, 28 and 26 are inside the range and both ends are included. 31 and 22 fall outside.
Show answer
B — 3 — 23, 28 and 26 are inside the range and both ends are included. 31 and 22 fall outside.
How do you refer to a Python variable inside query()?
The @ prefix tells query to look up min_score in the surrounding Python scope.
Show answer
A — Put an @ in front of the name, as in score >= @min_score — The @ prefix tells query to look up min_score in the surrounding Python scope.
Next
Once you can pull out the rows you want, the next skill is reordering them — continue to Sorting and Ranking.