This code creates a small table of people with their age, city, and score. Then it shows how to filter rows using AND and OR conditions.
Pandas
import pandas as pd
# Create sample data
data = {
'name': ['Alice', 'Bob', 'Charlie', 'David', 'Eva'],
'age': [25, 35, 30, 40, 22],
'city': ['New York', 'Los Angeles', 'New York', 'Chicago', 'Los Angeles'],
'score': [85, 90, 70, 60, 95]
}
df = pd.DataFrame(data)
# Filter: age > 30 AND city is Los Angeles
filtered_and = df[(df['age'] > 30) & (df['city'] == 'Los Angeles')]
# Filter: score < 70 OR city is Chicago
filtered_or = df[(df['score'] < 70) | (df['city'] == 'Chicago')]
print('Rows with age > 30 AND city = Los Angeles:')
print(filtered_and)
print('\nRows with score < 70 OR city = Chicago:')
print(filtered_or)
OutputSuccess
Important Notes
Always put each condition inside parentheses when combining with & or |.
Use & for AND, | for OR, not the words 'and' or 'or'.
Make sure your DataFrame columns exist to avoid errors.
Summary
Use & to combine conditions that must all be true (AND).
Use | to combine conditions where any can be true (OR).
Always wrap each condition in parentheses to avoid mistakes.
Practice
(1/5)
1. Which operator should you use in pandas to select rows where age is greater than 30 andsalary is above 50000?
easy
A. Use and to combine the two conditions without parentheses.
B. Use | to combine the two conditions without parentheses.
C. Use & to combine the two conditions with parentheses around each.
D. Use or to combine the two conditions with parentheses.
Solution
Step 1: Understand the condition type
We want rows where both conditions are true, so this is an AND condition.
Step 2: Choose the correct pandas operator
In pandas, & is used for AND between conditions, and each condition must be in parentheses.
Final Answer:
Use & to combine the two conditions with parentheses around each. -> Option C
Quick Check:
AND condition = & with parentheses [OK]
Hint: Use & with parentheses for AND conditions in pandas [OK]
Common Mistakes:
Using 'and' instead of & causes errors.
Not wrapping each condition in parentheses.
Using | instead of & for AND conditions.
2. Which of the following is the correct syntax to filter a DataFrame df for rows where df['A'] > 10 or df['B'] < 5?
easy
A. df[(df['A'] > 10) | (df['B'] < 5)]
B. df[(df['A'] > 10) & (df['B'] < 5)]
C. df[df['A'] > 10 | df['B'] < 5]
D. df[df['A'] > 10 or df['B'] < 5]
Solution
Step 1: Identify the condition type
The question asks for rows where either condition is true, so this is an OR condition.
Step 2: Use correct pandas syntax for OR
In pandas, | is used for OR, and each condition must be wrapped in parentheses inside the DataFrame filter.
Final Answer:
df[(df['A'] > 10) | (df['B'] < 5)] -> Option A
Quick Check:
OR condition = | with parentheses [OK]
Hint: Use | with parentheses for OR conditions in pandas [OK]
Step 2: Select rows where both conditions are true
Rows 1 and 2 satisfy both conditions.
Final Answer:
Rows with age > 30 and salary >= 50000: rows 1 and 2 -> Option B
Quick Check:
Both conditions true for rows 1 and 2 [OK]
Hint: Check each condition row-wise, then combine with & [OK]
Common Mistakes:
Confusing & with | and selecting wrong rows.
Ignoring parentheses causing syntax errors.
Misreading >= as > or vice versa.
4. What is wrong with this pandas filter code?
df[(df['score'] > 80) & df['age'] < 30]
Choose the best fix.
medium
A. Use 'and' instead of &.
B. Replace & with | to fix the syntax.
C. Remove parentheses around the first condition.
D. Add parentheses around the second condition: df[(df['score'] > 80) & (df['age'] < 30)]
Solution
Step 1: Identify the syntax error
The second condition df['age'] < 30 is not wrapped in parentheses, which is required when using &.
Step 2: Correct the syntax by adding parentheses
Wrap the second condition in parentheses to fix the error: df[(df['score'] > 80) & (df['age'] < 30)]
Final Answer:
Add parentheses around the second condition: df[(df['score'] > 80) & (df['age'] < 30)] -> Option D
Quick Check:
Each condition must be in parentheses with & [OK]
Hint: Wrap each condition in parentheses when using & or | [OK]
Common Mistakes:
Not using parentheses around all conditions.
Using 'and' instead of &.
Changing & to | without reason.
5. You have a DataFrame df with columns temperature and humidity. You want to select rows where temperature is between 20 and 30 inclusive, or humidity is below 40. Which of the following code snippets correctly applies these conditions?