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.
)
&w=1200&q=75)
&w=1200&q=75)
&w=1200&q=75)