0
0
Pandasdata~15 mins

str.contains() for pattern matching in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print(filtered_products) to display the filtered DataFrame.