Data Science

Filtering Data with Boolean Indexing

SQL Mastery Team
May 6, 2026
5 min read

Welcome to **Day 105**. In SQL, you use the `WHERE` clause. In Pandas, we use **Boolean Indexing**.

The Logic

When you write `df['age'] > 30`, Pandas creates a list of `True` and `False` values for every row. You then "mask" your DataFrame with this list.

Single Condition

# Filter for users in London

london_users = df[df['city'] == 'London']

Multiple Conditions (The Logic Trap)

In Python, we usually use `and` / `or`. In Pandas, you MUST use `&` / `|` and wrap your conditions in **parentheses**.

# Filter: London AND age over 25

filtered = df[(df['city'] == 'London') & (df['age'] > 25)]

Why the Parentheses?

Python's operator precedence rules are different for bitwise operators like `&`. Without parentheses, Pandas will get confused and throw an error.

Your Task for Today

Filter a sample dataset for two conditions simultaneously (e.g., Category = 'Tech' AND Price < 500).

*Day 106: Selecting Columns and Slicing Data.*

Ready to put your knowledge into practice?

Join SQL Mastery and learn through interactive exercises.