How to Filter pandas Rows by Two Conditions with & (and)
To keep the rows where two things are both true, such as orders over 50 that came from the UK, pandas joins two conditions with &. Each condition has to sit in its own brackets, and Python's own and doesn't work here. This guide shows the right form and the two errors the wrong ones give, with pandas 3.1.2 on Python 3.15.2.
Syntax
<rows> = <table>[(<condition 1>) & (<condition 2>)]
| Part | What it means |
|---|---|
(<condition 1>) | A condition such as orders["amount"] > 50, in its own brackets |
& | And: both conditions must be true. Use | for or, and ~ in front of a condition for not |
The indexing guide explains the brackets: "The operators are: | for or, & for and, and ~ for not. These must be grouped by using parentheses, since by default Python will evaluate an expression such as df['A'] > 2 & df['B'] < 3 as df['A'] > (2 & df['B']) < 3".
The Example
A table holds four orders with an amount and a country. The script keeps the orders over 50 from the UK.
Prerequisites
- pandas in your project, for example with
uv add pandas. This guide uses pandas 3.1.2 on Python 3.15.2.
Step 1: Write the Script
import pandas as pd
orders = pd.DataFrame(
{"amount": [30, 75, 120, 60], "country": ["UK", "UK", "FR", "FR"]}
)
big_uk = orders[(orders["amount"] > 50) & (orders["country"] == "UK")]
print(big_uk)
![The two_conditions.py script in a macOS-style code window: an orders DataFrame with amount and country, and big_uk = orders[(orders["amount"] > 50) & (orders["country"] == "UK")].](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/programming/python/2026/pandas-two-conditions-01-code.webp)
Step 2: Run It
uv run python two_conditions.py
amount country
1 75 UK

Only order 1 is both over 50 and from the UK. Order 0 is from the UK but only 30; orders 2 and 3 are over 50 but from France. The command works unchanged in PowerShell.
Or and Not
With | instead, (orders["amount"] > 100) | (orders["country"] == "UK") kept every order that is over 100 or from the UK:
amount country
0 30 UK
1 75 UK
2 120 FR
And ~ turns a condition around: orders[~(orders["country"] == "UK")] kept the orders that aren't from the UK, rows 2 and 3.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Python's and: (orders["amount"] > 50) and (orders["country"] == "UK") | ValueError: The truth value of a Series is ambiguous. Use a.empty, a.item(), a.any() or a.all(). | Use & |
Leaving out the brackets: orders["amount"] > 50 & orders["country"] == "UK" | TypeError: unsupported operand type(s) for &: 'int' and 'StringArray', because Python runs 50 & orders["country"] first | Put each condition in brackets |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
The truth value of a Series is ambiguous | and, or or not between two conditions | Use &, | and ~ |
unsupported operand type(s) for & | A condition without brackets around it | Bracket each condition |
FAQ
Why doesn't and work?
and asks for one True or False for each side, but a condition on a column has one per row, and pandas won't guess which you mean. & combines the two columns row by row.
Can I join more than two conditions?
Yes, keep adding them, each in brackets: (a) & (b) & (c).
Conclusion
orders[(orders["amount"] > 50) & (orders["country"] == "UK")] kept the one order that met both conditions. Use &, | and ~ instead of and, or and not, and put each condition in brackets.
Additional Resources
- Boolean indexing — the operators and why they need brackets.
- Operator precedence — why
&runs before>in Python.
