Python Central HubSign in

Data Analytics with Python / Phase 3: Data Manipulation with Pandas

Filtering with Conditions (and, or, isin, query)

Learn practical filtering patterns in Pandas using boolean masks, isin(), between(), and query().

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.

Python
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

Python
print(df[df["age"] >= 25])

Multiple conditions: AND / OR

Pandas uses bitwise operators:

  • AND: &
  • OR: |

Always use parentheses:

Python
mask = (df["city"] == "Delhi") & (df["score"] >= 80)
print(df[mask])
Python
mask = (df["city"] == "Mumbai") | (df["score"] >= 90)
print(df[mask])

isin() for membership checks

Python
mask = df["city"].isin(["Pune", "Delhi"])
print(df[mask])

between() for range filters

Python
mask = df["age"].between(23, 30, inclusive="both")
print(df[mask])

query() for readable filters

query() can be easier to read, especially in notebooks.

Python
result = df.query('city == "Delhi" and score >= 80')
print(result)

Variables inside query

Python
min_score = 85
result = df.query('score >= @min_score')
print(result)

Common mistakes

  • Using and / or instead of & / | (will error)
  • Forgetting parentheses around conditions
  • Comparing strings with extra spaces (clean using .str.strip())

Visualize it

diagram Boolean mask filtering mermaid
A condition produces a True/False mask; indexing with that mask keeps only the True rows.
sketch A condition becomes a keep/drop mask p5.js
Each row is tested against the condition; only rows where the mask is True survive the filter.

🧪 Try It Yourself

Exercise 1 – Build a Boolean Mask

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Combine Conditions With &

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Membership With isin

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Check your understanding

quizCheck yourself
  1. Which operators combine conditions in a pandas boolean filter?

    Show answer

    B — & and | — Pandas uses the bitwise operators & and |, with parentheses around each condition. Using and or or raises an error.

  2. In the sample data, which rows match (df["city"] == "Delhi") & (df["score"] >= 80)?

    Show answer

    A — Ravi only — Ravi and Meera live in Delhi, but Meera scored 77, so only Ravi (91) passes both tests.

  3. How many rows does df["age"].between(23, 30, inclusive="both") select for ages 23, 28, 26, 31 and 22?

    Show answer

    B — 3 — 23, 28 and 26 are inside the range and both ends are included. 31 and 22 fall outside.

  4. How do you refer to a Python variable inside query()?

    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.

Was this page helpful?

Let us know how we did