How to Select Rows with Multiple Conditions in pandas
To select rows with multiple conditions in pandas, use the
DataFrame.loc accessor combined with conditions inside parentheses and joined by logical operators like & (and) or | (or). Each condition must be enclosed in parentheses to ensure correct evaluation.Syntax
Use df.loc[condition] where condition combines multiple boolean expressions with & (and) or | (or). Each condition must be wrapped in parentheses.
df: your DataFramecondition1,condition2: boolean expressions likedf['col'] > 5&: logical AND|: logical OR
python
df.loc[(condition1) & (condition2)] # AND condition df.loc[(condition1) | (condition2)] # OR condition
Example
This example shows how to select rows where the 'age' is greater than 25 and 'score' is at least 80.
python
import pandas as pd data = {'name': ['Alice', 'Bob', 'Charlie', 'David'], 'age': [24, 30, 22, 35], 'score': [85, 78, 90, 88]} df = pd.DataFrame(data) # Select rows where age > 25 AND score >= 80 filtered_df = df.loc[(df['age'] > 25) & (df['score'] >= 80)] print(filtered_df)
Output
name age score
3 David 35 88
Common Pitfalls
Common mistakes include forgetting to wrap each condition in parentheses or using and/or instead of &/|. This causes errors or unexpected results.
Wrong example:
df.loc[(df['age'] > 25) and (df['score'] >= 80)]
Right example:
df.loc[(df['age'] > 25) & (df['score'] >= 80)]
python
import pandas as pd data = {'age': [20, 30], 'score': [90, 85]} df = pd.DataFrame(data) # Wrong: causes error # df.loc[(df['age'] > 25) and (df['score'] >= 80)] # Correct: filtered = df.loc[(df['age'] > 25) & (df['score'] >= 80)] print(filtered)
Output
age score
1 30 85
Quick Reference
| Operator | Meaning | Example |
|---|---|---|
| & | Logical AND | df.loc[(df['A'] > 5) & (df['B'] < 10)] |
| | | Logical OR | df.loc[(df['A'] > 5) | (df['B'] < 10)] |
| ~ | Logical NOT | df.loc[~(df['A'] > 5)] |
Key Takeaways
Use parentheses around each condition when combining multiple conditions in pandas.
Use & for AND and | for OR between conditions, not and/or keywords.
Use df.loc[...] to filter rows based on conditions.
Logical NOT can be done with ~ before a condition.
Incorrect syntax causes errors or wrong filtering results.