Filter with str.contains()
Intermediate · Write the code · from Pandas String and DateTime Operations for Real-World Data
From the lesson: Pandas String and DateTime Operations for Real-World Data
Real-world data is messy. Names arrive in ALL CAPS, dates come as random text strings like "Jan 5, 2024" or "2024/01/05", and phone numbers have dashes in some rows but not others. Before you can analyze anything, you need to clean this stuff up.
Read the lessonFilter the DataFrame to keep only rows where the email column contains "gmail". Print the name column of the filtered rows as a list.
import pandas as pd
df = pd.DataFrame({
'name': ['Alice', 'Bob', 'Carol', 'Dave'],
'email': ['alice@gmail.com', 'bob@yahoo.com', 'carol@gmail.com', 'dave@outlook.com']
})
# Filter for Gmail users and print their names
More Pandas String and Datetime exercises
- Clean Messy NamesBeginner
- Predict the String Operation OutputIntermediate
- Parse Dates and Extract MonthIntermediate
- Calculate Days Between DatesIntermediate
- Fix the String Extraction BugIntermediate
All Python exercises · 50 practice problems · Data Science & Machine Learning quiz