๐Ÿ€Zerve chosen as NCAA's Agentic Data Platform for 2026 Hackathonยท๐ŸงฎMeet the Zerve Team at Data Decoded Londonยท๐Ÿ“ˆWe're hiring โ€” awesome new roles just gone live!
Back
Pandas

How to Filter Dataframe by Multiple Conditions in Pandas

Answer

Use & for AND and | for OR, with each condition wrapped in parentheses. Don't use Python's and/or keywords โ€” they don't work with pandas boolean arrays.

Why This Happens

Single-condition filters are easy, but real analysis requires combining them: "customers over 30 who spent more than $100" or "orders from Q1 or Q4". Getting the syntax wrong causes errors or silently wrong results.

Solution

The rule: always use &/| (not and/or), always wrap each condition in parentheses, or use .query() for readability.

import pandas as pd

df = pd.DataFrame({
    'name': ['Alice', 'Bob', 'Charlie', 'David'],
    'age': [25, 35, 45, 30],
    'spent': [150, 80, 200, 120]
})

# โŒ Problematic: using 'and' keyword
df[df['age'] > 30 and df['spent'] > 100]
# ValueError: The truth value of a Series is ambiguous

# โœ… Fixed: use & with parentheses for AND
df[(df['age'] > 30) & (df['spent'] > 100)]

# โœ… Use | for OR
df[(df['age'] > 40) | (df['spent'] > 150)]

# โœ… Use ~ for NOT
df[~(df['age'] > 30)]  # age <= 30

# โœ… Combine multiple conditions
df[(df['age'] > 25) & (df['age'] < 40) & (df['spent'] > 100)]

# โœ… Use .query() for cleaner syntax
df.query('age > 30 and spent > 100')
df.query('age > 40 or spent > 150')

# โœ… Use .isin() for multiple values
df[df['name'].isin(['Alice', 'Bob'])]

Better Workflow

Zerve persists your dataframe state at the cell level, so you can experiment with different filter conditions without fear of corrupting your session. No need to re-run everything if you mess up โ€” your previous state is always there.

Better workflow

Related Topics

Decision-grade data work

Explore, analyze and deploy your first project in minutes