0
0
Pandasdata~30 mins

Regex operations in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Regex operations in Pandas
📖 Scenario: You work in a company that collects customer feedback. The feedback contains various comments with different formats. You want to find comments that mention specific keywords like 'happy' or 'satisfied' to understand positive feedback.
🎯 Goal: Build a small program using pandas to filter comments containing the words 'happy' or 'satisfied' using regex operations.
📋 What You'll Learn
Create a pandas DataFrame with a column named comments containing specific text entries.
Create a regex pattern string to find the words 'happy' or 'satisfied'.
Use pandas str.contains() method with the regex pattern to filter the DataFrame.
Print the filtered DataFrame showing only comments with the keywords.
💡 Why This Matters
🌍 Real World
Companies often analyze customer feedback to find positive or negative comments using text search with regex.
💼 Career
Data analysts and data scientists use pandas and regex to clean and filter text data for reports and insights.
Progress0 / 4 steps
1
Create the DataFrame with comments
Import pandas as pd and create a DataFrame called df with a column named comments containing these exact strings: 'I am very happy with the service', 'The product is okay', 'Customer support was satisfied', 'Not happy with the delivery time', 'Will buy again'.
Pandas
Need a hint?

Use pd.DataFrame and pass a dictionary with key 'comments' and list of strings as value.

2
Create the regex pattern
Create a string variable called pattern that holds the regex pattern to find the words 'happy' or 'satisfied' exactly.
Pandas
Need a hint?

Use the regex 'happy|satisfied' to match either word.

3
Filter comments using regex
Use the pandas str.contains() method on the comments column with the regex pattern stored in pattern to create a new DataFrame called filtered_df containing only rows where the comment matches the pattern. Use regex=True and case=False to ignore case.
Pandas
Need a hint?

Use df['comments'].str.contains(pattern, regex=True, case=False) inside df[ ... ] to filter rows.

4
Print the filtered comments
Print the filtered_df DataFrame to show only the comments containing 'happy' or 'satisfied'.
Pandas
Need a hint?

Use print(filtered_df) to display the filtered comments.