What does the str.contains() method do in Pandas when used with regex?
It checks each string in a Series to see if it contains a pattern defined by a regular expression, returning a Series of True or False values.
Click to reveal answer
beginner
How do you use regex to extract a pattern from a Pandas Series?
Use the str.extract() method with a regex pattern. It returns a DataFrame with the matched groups.
Click to reveal answer
beginner
What is the purpose of the str.replace() method with regex in Pandas?
It replaces parts of strings that match a regex pattern with a specified replacement string.
Click to reveal answer
intermediate
How can you filter rows in a DataFrame where a column matches a regex pattern?
Use df[df['column'].str.contains('pattern', regex=True)] to keep rows where the pattern is found.
Click to reveal answer
intermediate
What does setting na=False do in str.contains()?
It treats missing values (NaN) as False, so they don't cause errors and are excluded from matches.
Click to reveal answer
Which Pandas method extracts regex groups from a Series?
Astr.extract()
Bstr.contains()
Cstr.replace()
Dstr.match()
✗ Incorrect
str.extract() pulls out matching groups from strings using regex.
What does str.contains('abc', regex=True) return?
AReplaces 'abc' with True
BA Series of strings containing 'abc'
CA DataFrame with extracted 'abc' matches
DA Series of True/False indicating if 'abc' is in each string
✗ Incorrect
It returns True or False for each string depending on if 'abc' is found.
How do you replace all digits in a Series with '#' using regex?
Astr.extract('\d')
Bstr.replace('#', '\d', regex=True)
Cstr.replace('\d', '#', regex=True)
Dstr.contains('\d')
✗ Incorrect
Use str.replace() with regex pattern '\d' to replace digits.
What happens if you use str.contains() on a Series with NaN values without na=False?
ARaises an error
BReturns False for NaN values
CIgnores NaN values
DReturns True for NaN values
✗ Incorrect
By default, it returns NaN for NaN values, not an error. Use na=False to treat NaN as False.
Which method would you use to check if a string starts with a pattern using regex?
Astr.contains()
Bstr.match()
Cstr.extract()
Dstr.replace()
✗ Incorrect
str.match() checks if the entire string matches a regex pattern, often used for start matches.
Explain how to filter a Pandas DataFrame column to keep only rows where the text matches a regex pattern.
Think about using a method that returns True/False for each row and then selecting rows.
You got /4 concepts.
Describe how to extract parts of strings from a Pandas Series using regex groups.
Focus on the method that pulls out matching parts instead of just checking presence.
You got /4 concepts.
Practice
(1/5)
1. What does the str.contains method do in Pandas when used with a regex pattern?
easy
A. It extracts the first match of the regex pattern from each string.
B. It replaces all matches of the regex pattern with a new string.
C. It sorts the DataFrame rows based on regex matches.
D. It checks if each string in a column contains the regex pattern and returns true or false.
Solution
Step 1: Understand str.contains usage
str.contains checks if the regex pattern appears anywhere in each string of the column and returns a boolean Series.
Step 2: Differentiate from other methods
str.replace changes text, str.extract pulls out matches, and sorting is unrelated.
Final Answer:
It checks if each string in a column contains the regex pattern and returns true or false. -> Option D
Quick Check:
str.contains = Boolean check [OK]
Hint: Contains returns true/false if pattern found in each string [OK]
Common Mistakes:
Confusing contains with replace or extract
Thinking contains modifies the data
Assuming contains returns matched text
2. Which of the following is the correct syntax to replace every sequence of consecutive digits in a Pandas column df['col'] with the character 'X' using regex?
easy
A. df['col'].str.replace('\d+', 'X')
B. df['col'].str.replace('[0-9]', 'X')
C. df['col'].replace('[0-9]', 'X')
D. df['col'].str.extract('[0-9]', 'X')
Solution
Step 1: Identify correct method for regex replacement
Use str.replace on the Series to replace regex matches. df['col'].replace('[0-9]', 'X') is wrong because replace without str is for exact values, not regex.
Step 2: Check regex pattern and syntax
df['col'].str.replace('[0-9]', 'X') matches single digits only ('123' -> 'XXX'). df['col'].str.replace('\d+', 'X') matches sequences of one or more digits ('123' -> 'X').
Final Answer:
df['col'].str.replace('\d+', 'X') -> Option A
Quick Check:
Use str.replace with regex pattern [OK]
Hint: Use str.replace with regex pattern and replacement string [OK]
Common Mistakes:
Using DataFrame replace instead of Series.str.replace
A. The regex pattern misses the last digit, so match returns false for all rows.
B. The code raises a SyntaxError due to incorrect regex syntax.
C. The method str.match does not accept regex patterns.
D. The code runs correctly and returns true for all rows.
Solution
Step 1: Analyze the regex pattern
The pattern [a-z]+\d matches one or more lowercase letters followed by exactly one digit from the start of the string.
Step 2: Compare pattern with data strings
The strings 'abc123', 'def456', 'ghi789' have multiple digits at the end, but the pattern only expects one digit. Since str.match matches from the start and the pattern does not cover the full digit sequence, it returns False for all rows.
Final Answer:
The regex pattern misses the last digit, so match returns false for all rows. -> Option A
Quick Check:
str.match matches from start; pattern must cover full prefix for True [OK]
Hint: str.match anchors at start (prefix match), pattern must cover full prefix [OK]
Common Mistakes:
Thinking str.match requires full string match
Confusing str.match with str.fullmatch
Thinking regex syntax is invalid here
5. You have a DataFrame df with a column 'emails' containing email addresses mixed with invalid entries. How would you create a new column 'valid' that is true only for rows where 'emails' contains a valid email pattern using regex in Pandas?
hard
A. df['valid'] = df['emails'].str.extract(r'^[\w\.-]+@[\w\.-]+\.\w+$')
B. df['valid'] = df['emails'].str.contains(r'^[\w\.-]+@[\w\.-]+\.\w+$')
C. df['valid'] = df['emails'].str.replace(r'^[\w\.-]+@[\w\.-]+\.\w+$', 'true')
D. df['valid'] = df['emails'].str.match(r'\w+@\w+')
Solution
Step 1: Choose method to check pattern presence
str.contains returns true/false if regex matches anywhere in string, suitable for validation.
Step 2: Use correct regex for full email validation
The regex ^[\w\.-]+@[\w\.-]+\.\w+$ matches a full email pattern from start to end. df['valid'] = df['emails'].str.extract(r'^[\w\.-]+@[\w\.-]+\.\w+$') extracts matched text, not boolean. df['valid'] = df['emails'].str.replace(r'^[\w\.-]+@[\w\.-]+\.\w+$', 'true') replaces text, not boolean. df['valid'] = df['emails'].str.match(r'\w+@\w+') uses str.match but regex is incomplete and may match partial emails.
Final Answer:
df['valid'] = df['emails'].str.contains(r'^[\w\.-]+@[\w\.-]+\.\w+$') -> Option B
Quick Check:
Use str.contains with full regex for validation [OK]
Hint: Use str.contains with full regex pattern for true/false validation [OK]
Common Mistakes:
Using str.extract which returns strings, not booleans
Using str.replace which changes data instead of checking
Using incomplete regex that matches partial emails
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.