Filter Products Using str.contains() for Pattern Matching
📖 Scenario: You work in a store's data team. You have a list of products with their names and prices. Your manager wants to find all products that have the word "apple" anywhere in their name, ignoring case. This helps the store quickly see all apple-related products.
🎯 Goal: Build a small program that uses str.contains() to filter product names containing the word "apple" (case-insensitive) and display the filtered list.
📋 What You'll Learn
Create a pandas DataFrame called products with columns name and price using the exact data provided.
Create a variable called pattern that holds the string 'apple'.
Use str.contains() with pattern and case=False to filter the DataFrame.
Print the filtered DataFrame.
💡 Why This Matters
🌍 Real World
Stores and businesses often need to search product lists for specific keywords to analyze or promote certain items quickly.
💼 Career
Data analysts and data scientists use pattern matching in text data to filter and analyze relevant information efficiently.
Progress0 / 4 steps
1
Create the products DataFrame
Create a pandas DataFrame called products with two columns: name and price. Use these exact entries: 'Green Apple' priced 1.2, 'Banana' priced 0.5, 'Red Apple' priced 1.5, 'Orange' priced 0.8, and 'Pineapple' priced 3.0.
Pandas
Hint
Use pd.DataFrame with a dictionary where keys are column names and values are lists of data.
2
Set the pattern variable
Create a variable called pattern and set it to the string 'apple'.
Pandas
Hint
Just assign the string 'apple' to a variable named pattern.
3
Filter products using str.contains()
Use str.contains() on the name column of products with the pattern variable and case=False to create a filtered DataFrame called filtered_products that only has rows where the product name contains "apple" ignoring case.
Pandas
Hint
Use products['name'].str.contains(pattern, case=False) inside the DataFrame indexing brackets to filter rows.
4
Print the filtered products
Print the filtered_products DataFrame to show the products whose names contain "apple" ignoring case.
Pandas
Hint
Use print(filtered_products) to display the filtered DataFrame.
Practice
(1/5)
1. What does the str.contains() method do in pandas?
easy
A. It checks if a string pattern exists in each element of a Series.
B. It replaces strings matching a pattern with another string.
C. It counts the number of times a pattern appears in a Series.
D. It sorts the Series based on string length.
Solution
Step 1: Understand the purpose of str.contains()
This method is used to check if each string element in a pandas Series contains a specific pattern.
Step 2: Compare with other string methods
Replacing strings or counting occurrences are done by other methods like str.replace() or str.count(). Sorting is unrelated.
Final Answer:
It checks if a string pattern exists in each element of a Series. -> Option A
Quick Check:
str.contains() = check pattern presence [OK]
Hint: Remember: contains means 'is pattern inside?' [OK]
Common Mistakes:
Confusing contains with replace
Thinking it counts occurrences
Assuming it sorts data
2. Which of the following is the correct syntax to find rows where column 'Name' contains the substring 'John' (case insensitive) in a DataFrame df?
easy
A. df[df['Name'].str.contains('John', ignore_case=True)]
B. df[df['Name'].contains('John', case=False)]
C. df[df['Name'].str.contains('John', case=True)]
D. df[df['Name'].str.contains('John', case=False)]
Solution
Step 1: Identify correct method and parameters
The method is str.contains() and to ignore case, use case=False.
Step 2: Check syntax correctness
df[df['Name'].str.contains('John', case=False)] uses correct method and parameter. df[df['Name'].contains('John', case=False)] misses str. df[df['Name'].str.contains('John', ignore_case=True)] uses wrong parameter ignore_case. df[df['Name'].str.contains('John', case=True)] sets case=True, which is case sensitive.
Final Answer:
df[df['Name'].str.contains('John', case=False)] -> Option D
Quick Check:
Use str.contains() with case=False [OK]
Hint: Use case=False for ignoring case in str.contains() [OK]
Common Mistakes:
Forgetting to use str accessor
Using wrong parameter name like ignore_case
Setting case=True when case insensitive needed
3. Given the DataFrame df below, what is the output of df['City'].str.contains('York', na=False)?
'New York' contains 'York' (True), 'Yorkshire' contains 'York' (True), None is missing so replaced by False due to na=False, 'Los Angeles' does not contain 'York' (False).
Step 2: Construct boolean Series output
The result is [True, True, False, False].
Final Answer:
[True, True, False, False] -> Option C
Quick Check:
Check each string for 'York' with na=False [OK]
Hint: Use na=False to handle missing values safely [OK]
Common Mistakes:
Ignoring missing values causing errors
Assuming None matches pattern
Misreading which strings contain 'York'
4. What is wrong with this code snippet?
df[df['Email'].str.contains('@gmail.com')]
Assume some values in 'Email' column are missing (NaN).
medium
A. It will return all rows because '@gmail.com' matches everything.
B. It will raise an error due to missing values without handling na parameter.
C. It will ignore case sensitivity by default causing wrong results.
D. It will convert all emails to lowercase automatically.
Solution
Step 1: Understand missing data impact on str.contains()
If the column has NaN values, str.contains() raises an error unless na parameter is set.
Step 2: Check code for na parameter
The code does not set na=False, so it will raise an error when encountering NaN.
Final Answer:
It will raise an error due to missing values without handling na parameter. -> Option B
Quick Check:
Missing na causes errors with NaN [OK]
Hint: Always add na=False to avoid errors with missing data [OK]
Common Mistakes:
Ignoring NaN causes runtime error
Assuming default ignores case or missing data
Thinking it auto-lowers case
5. You have a DataFrame df with a column 'Description' containing product details. You want to filter rows where 'Description' contains either 'red' or 'blue' (case insensitive), but some rows have missing values. Which code snippet correctly does this?
hard
A. df[df['Description'].str.contains('red|blue', case=False, na=False)]
B. df[df['Description'].str.contains('red|blue', case=True)]
C. df[df['Description'].str.contains('red|blue', na=True)]
D. df[df['Description'].str.contains('red|blue', case=False)]
Solution
Step 1: Use regex pattern for multiple options
To match 'red' or 'blue', use the regex pattern 'red|blue'.
Step 2: Handle case insensitivity and missing values
Set case=False for ignoring case and na=False to avoid errors on missing data.
Step 3: Verify options
df[df['Description'].str.contains('red|blue', case=False, na=False)] correctly uses both case=False and na=False. df[df['Description'].str.contains('red|blue', case=True)] is case sensitive. df[df['Description'].str.contains('red|blue', na=True)] uses invalid na=True. df[df['Description'].str.contains('red|blue', case=False)] misses na=False.
Final Answer:
df[df['Description'].str.contains('red|blue', case=False, na=False)] -> Option A
Quick Check:
Use regex with case=False and na=False [OK]
Hint: Use regex 'red|blue' with case=False and na=False [OK]
Common Mistakes:
Forgetting na=False causes errors
Using case sensitive search unintentionally
Wrong regex or missing regex for multiple patterns
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.