Filter Data with Multiple Conditions Using & and | in pandas
📖 Scenario: You work in a small online store. You have a list of products with their prices and stock counts. You want to find products that meet certain conditions to decide what to promote or reorder.
🎯 Goal: Build a pandas DataFrame with product data, set a price threshold, filter products using multiple conditions combined with & (and) and | (or), and display the filtered results.
📋 What You'll Learn
Create a pandas DataFrame named products with columns Product, Price, and Stock using the exact data provided.
Create a variable price_limit with the exact value 20.
Use multiple conditions combined with & and | to filter products where Price is less than price_limit and Stock is greater than 10, or where Product is exactly 'Notebook'.
Store the filtered DataFrame in a variable named filtered_products.
Print the filtered_products DataFrame.
💡 Why This Matters
🌍 Real World
Filtering data with multiple conditions is common in business to find products that meet specific criteria, like affordable price and enough stock.
💼 Career
Data analysts and scientists often filter datasets using multiple conditions to prepare data for reports, dashboards, or decision-making.
Progress0 / 4 steps
1
Create the products DataFrame
Create a pandas DataFrame called products with these exact columns and data: Product with values ['Pen', 'Notebook', 'Eraser', 'Pencil', 'Marker'], Price with values [5, 25, 3, 2, 15], and Stock with values [50, 5, 100, 20, 0].
Pandas
Hint
Use pd.DataFrame with a dictionary where keys are column names and values are lists of data.
2
Set the price limit
Create a variable called price_limit and set it to the integer value 20.
Pandas
Hint
Just assign the number 20 to the variable price_limit.
3
Filter products with multiple conditions
Create a variable called filtered_products that filters products where Price is less than price_limit and Stock is greater than 10, or where Product is exactly 'Notebook'. Use parentheses and combine conditions with & and |.
Pandas
Hint
Use parentheses around each condition and combine them with & for AND and | for OR.
4
Print the filtered products
Print the filtered_products DataFrame to display the filtered results.
Pandas
Hint
Use print(filtered_products) to show the filtered DataFrame.
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?