📖 Scenario: You work in a small store and have a list of products with their prices. You want to find which products are affordable for customers who want to spend less than a certain amount.
🎯 Goal: Build a program that filters products based on a price limit using boolean filtering.
📋 What You'll Learn
Create a dictionary with product names as keys and prices as values
Create a variable for the price limit
Use boolean filtering to select products cheaper than the price limit
Print the filtered products
💡 Why This Matters
🌍 Real World
Filtering data based on conditions is common in stores, websites, and apps to show users only relevant items within their budget.
💼 Career
Data filtering is a basic skill for data analysts and scientists to prepare and analyze data efficiently.
Progress0 / 4 steps
1
Create the product price dictionary
Create a dictionary called products with these exact entries: 'apple': 120, 'banana': 80, 'cherry': 150, 'date': 60, 'elderberry': 200.
Data Analysis Python
Hint
Use curly braces to create a dictionary with product names as keys and prices as values.
2
Set the price limit
Create a variable called price_limit and set it to 100.
Data Analysis Python
Hint
Just assign the number 100 to the variable price_limit.
3
Filter products cheaper than the price limit
Create a dictionary called affordable_products using a dictionary comprehension that includes only products from products with prices less than price_limit. Use for product, price in products.items() in your comprehension.
Data Analysis Python
Hint
Use a dictionary comprehension with an if condition to filter products.
4
Print the affordable products
Print the affordable_products dictionary.
Data Analysis Python
Hint
Use print(affordable_products) to show the filtered dictionary.
Practice
(1/5)
1. What does boolean filtering do in data analysis with Python?
easy
A. Changes the data type of columns
B. Removes duplicate rows from the data
C. Sorts the data in ascending order
D. Selects rows where a condition is true
Solution
Step 1: Understand boolean filtering purpose
Boolean filtering is used to pick rows that meet a true condition.
Step 2: Compare options to definition
Only Selects rows where a condition is true describes selecting rows based on true conditions.
Final Answer:
Selects rows where a condition is true -> Option D
Hint: Boolean filtering picks rows where condition is true [OK]
Common Mistakes:
Confusing filtering with sorting
Thinking it changes data types
Assuming it removes duplicates
2. Which of the following is the correct syntax to filter rows where column age is greater than 30 in a DataFrame df?
easy
A. df['age' > 30]
B. df[df['age'] > 30]
C. df.age[df.age > 30]
D. df[df['age'] < 30]
Solution
Step 1: Identify correct boolean filtering syntax
The correct way is to put the condition inside square brackets after the DataFrame.
Step 2: Check each option
df[df['age'] > 30] uses df[df['age'] > 30], which is correct syntax. df['age' > 30] is invalid syntax. df.age[df.age > 30] incorrectly filters only the age column, returning a Series not DataFrame. df[df['age'] < 30] filters less than 30, not greater.
Final Answer:
df[df['age'] > 30] -> Option B
Quick Check:
Correct syntax = df[df['age'] > 30] [OK]
Hint: Use df[condition] with column in quotes for filtering [OK]
What will df[df['score'] > 80 and df['passed']] return?
medium
A. ValueError due to missing parentheses
B. Rows where score > 80 or passed is true
C. Rows where score > 80 and passed is true
D. Empty DataFrame
Solution
Step 1: Analyze the boolean expression
The expression uses and without parentheses around each condition, which causes a ValueError in pandas.
Step 2: Understand pandas boolean filtering rules
Each condition must be in parentheses when combined with & or |. So df['score'] > 80 and df['passed'] raises ValueError.
Final Answer:
ValueError due to missing parentheses -> Option A
Quick Check:
Use parentheses around conditions with & or | [OK]
Hint: Always wrap each condition in parentheses when using & or | [OK]
Common Mistakes:
Not using parentheses around conditions
Confusing & with and
Expecting output without fixing syntax
4. Consider this code snippet:
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3, 4]})
filtered = df[df['A'] > 2 or df['A'] < 4]
What is the error and how to fix it?
medium
A. ValueError: Use parentheses around conditions with or
B. SyntaxError: Missing colon after if
C. No error, code runs fine
D. ValueError: Cannot compare integers
Solution
Step 1: Identify the error cause
The expression df['A'] > 2 or df['A'] < 4 lacks parentheses around each condition, causing a ValueError.
Step 2: Fix the error by adding parentheses
Correct syntax is df[(df['A'] > 2) | (df['A'] < 4)] to ensure proper evaluation.
Final Answer:
ValueError: Use parentheses around conditions with or -> Option A
Quick Check:
Parentheses needed around each condition with | or & [OK]
Hint: Wrap each condition in parentheses when using | or & [OK]
Common Mistakes:
Forgetting parentheses around conditions
Using Python 'or' instead of |
Assuming no error with missing parentheses
5. You have a DataFrame df with columns age and income. You want to filter rows where age is between 25 and 40 inclusive, and income is above 50000. Which code correctly applies boolean filtering?
D. df[df['age'] >= 25 and df['age'] <= 40 and df['income'] > 50000]
Solution
Step 1: Understand the filtering conditions
We want age between 25 and 40 inclusive, so age >= 25 and age <= 40, and income > 50000.
Step 2: Check correct syntax for combining conditions
Conditions must be in parentheses and combined with &. df[(df['age'] >= 25) & (df['age'] <= 40) & (df['income'] > 50000)] correctly uses parentheses and & operators.
Step 3: Identify errors in other options
df[df['age'] >= 25 & df['age'] <= 40 & df['income'] > 50000] lacks parentheses around each condition, causing errors. df[(df['age'] > 25) | (df['age'] < 40) & (df['income'] > 50000)] uses | instead of & incorrectly. df[df['age'] >= 25 and df['age'] <= 40 and df['income'] > 50000] uses Python 'and' which does not work with pandas Series.