How do I select rows in a DataFrame by condition?
Short answer
Build a boolean condition on a column and pass it in square brackets. Combine conditions with the and-or operators for bitwise logic, wrapping each condition in brackets.
The short version
Write a condition on a column, which produces a column of True and False values, then pass it inside square brackets to keep only the True rows. This is called boolean indexing. To combine conditions, use the bitwise operators and wrap each condition in brackets, because Python's and and or do not work element by element.
A worked example
import pandas as pd
products = pd.DataFrame({
"product": ["pen", "mug", "pen", "cup"],
"price": [2, 5, 3, 4],
})
dear = products[products["price"] > 3]
print(dear)
product price
1 mug 5
3 cup 4
The expression products["price"] > 3 returns a boolean Series, and the outer brackets keep the rows where it is True.
Combining conditions correctly
Use & for and, | for or, and ~ for not. Each condition must be wrapped in its own brackets because these operators bind more tightly than the comparisons.
products[(products["price"] > 2) & (products["product"] == "pen")]
The mistake to avoid
Writing products["price"] > 2 and products["product"] == "pen" raises ValueError: The truth value of a Series is ambiguous. That is because and tries to reduce a whole column to a single True or False, which is undefined. Always use & and | for column-wise logic.
Matching against a set of values
For "is it one of these", use isin rather than chaining many | conditions.
products[products["product"].isin(["pen", "cup"])]
A more readable alternative
For long conditions, query lets you write the filter as a string, which some people find clearer.
products.query("price > 2 and product == 'pen'")
Inside query you can use plain and and or, because it is parsed as an expression rather than executed as Python operators. Use whichever form reads best for your team, but be consistent.