logo

Filtering with Multiple Conditions

Real questions often have multiple criteria. "Active users over 30" or "Sales in Q1 or Q2."

For AND logic (both conditions must be true), use &:

df[(df['age'] > 30) & (df['status'] == 'active')]

For OR logic (either condition), use |:

df[(df['city'] == 'NYC') | (df['city'] == 'LA')]

Parentheses around each condition are required - without them, Python's operator precedence causes errors.

You can chain multiple conditions:

df[(df['age'] > 25) & (df['age'] < 40) & (df['active'] == True)]

For complex multi-condition filters, see The Ultimate Pandas Bootcamp.