Skip to main content

How to Filter pandas Rows by Two Conditions with & (and)

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<rows> = <table>[(<condition 1>) & (<condition 2>)]
PartWhat 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​

two_conditions.py
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")].

Step 2: Run It​

Terminal
uv run python two_conditions.py
Output
amount country
1 75 UK

The terminal after uv run python two_conditions.py: one row, label 1, amount 75 and country 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:

Output
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​

MistakeWhat happensDo 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"] firstPut each condition in brackets

Troubleshooting​

SymptomLikely causeFix
The truth value of a Series is ambiguousand, or or not between two conditionsUse &, | and ~
unsupported operand type(s) for &A condition without brackets around itBracket 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​