Last time, we filtered data with one rule. But what if your question is more specific? For example, finding customers who are over 30 and live in London. This requires combining multiple conditions to narrow down your results.
Engagement Message
What insights become possible when you can combine multiple criteria?
To find rows that meet both Condition A AND Condition B, we use the &
(AND) operator. This tells Pandas to only keep rows where both conditions are true. It's how you narrow your search to be more precise.
Engagement Message
If you filter for Department == 'Sales'
& Region == 'West'
, would a sales employee from the 'East' region be included?
Let's see a real example. To find expensive electronics, you could write:
df[(df['Category'] == 'Electronics') & (df['Price'] > 500)]
This code selects only the rows that are both in the 'Electronics' category and cost more than $500.
Engagement Message
Can you think of another real-world question that would require an &
filter?
Here's the most important syntax rule: when using &
or |
, each individual condition must be wrapped in its own set of parentheses . This ensures Pandas evaluates each condition correctly before combining them.
