0
0
Data Analysis Pythondata~15 mins

Pattern matching with str.contains in Data Analysis Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Pattern Matching with str.contains in Data Science
📖 Scenario: You work in a small bookstore. You have a list of book titles and want to find which books mention the word "Python" in their titles. This helps you quickly find all Python-related books.
🎯 Goal: Build a simple program that uses pattern matching with str.contains to find book titles containing the word "Python".
📋 What You'll Learn
Create a pandas DataFrame with a column named title containing the exact book titles given.
Create a variable called pattern that holds the string 'Python'.
Use str.contains with the pattern variable to filter the DataFrame for titles containing "Python".
Print the filtered DataFrame showing only the matching book titles.
💡 Why This Matters
🌍 Real World
Finding specific keywords in text data is common in data science, such as searching product names, customer reviews, or document contents.
💼 Career
Pattern matching with <code>str.contains</code> is a basic skill for data cleaning, filtering, and text analysis used by data analysts and scientists.
Progress0 / 4 steps
1
Create the book titles DataFrame
Import pandas as pd and create a DataFrame called books with a column title containing these exact book titles: 'Learn Python Programming', 'Data Science Handbook', 'Python for Data Analysis', 'Cooking 101', 'Advanced Python Techniques'.
Data Analysis Python
Hint

Use pd.DataFrame with a dictionary where the key is 'title' and the value is a list of the exact book titles.

2
Create the pattern variable
Create a variable called pattern and set it to the string 'Python'.
Data Analysis Python
Hint

Just assign the string 'Python' to the variable pattern.

3
Filter titles using str.contains
Use str.contains with the variable pattern on the title column of books to create a new DataFrame called python_books that contains only the rows where the title includes "Python".
Data Analysis Python
Hint

Use books['title'].str.contains(pattern) inside the brackets to filter rows.

4
Print the filtered book titles
Print the DataFrame python_books to show only the book titles that contain the word "Python".
Data Analysis Python
Hint

Use print(python_books) to display the filtered DataFrame.