It extracts groups from strings in a pandas Series or DataFrame column using a regular expression (regex). It returns the matched groups as new columns.
Click to reveal answer
beginner
How do you specify which part of the string to extract using str.extract()?
You use parentheses () in the regex pattern to define capture groups. Only the text matched inside these groups is extracted.
Click to reveal answer
intermediate
What type of object does str.extract() return when extracting one group vs multiple groups?
If one group is extracted, it returns a Series. If multiple groups are extracted, it returns a DataFrame with one column per group.
Click to reveal answer
beginner
Why is it useful to use str.extract() with regex in data cleaning?
It helps pull out specific parts of messy text data, like extracting dates, codes, or names, making the data easier to analyze.
Click to reveal answer
intermediate
Example: What does df['col'].str.extract(r'(\d{3})-(\d{2})') extract?
It extracts two groups of digits separated by a dash: the first group with exactly 3 digits, and the second group with exactly 2 digits, returning them as two columns.
Click to reveal answer
What symbol in regex defines a capture group for str.extract()?
A()
B[]
C{}
D<>
✗ Incorrect
Parentheses () define capture groups in regex, which str.extract() uses to extract parts of strings.
If your regex has two capture groups, what does str.extract() return?
AA Series with one column
BA DataFrame with two columns
CA list of strings
DA single string
✗ Incorrect
Two capture groups produce a DataFrame with one column per group.
Which pandas object can you use str.extract() on?
ADataFrame
BSeries
CBoth DataFrame and Series
Dnull
✗ Incorrect
str.extract() is used on pandas Series (single columns). For DataFrames, you apply it on a column (Series).
What happens if the regex pattern does not match any part of the string?
AReturns the original string
BReturns NaN for that row
CRaises an error
DReturns an empty string
✗ Incorrect
If no match is found, str.extract() returns NaN for that row.
Which of these regex patterns extracts a 4-digit year from a string?
Ar'(\d{4})'
Br'\d{2}'
Cr'(\w{4})'
Dr'\d{5}'
✗ Incorrect
The pattern r'(\d{4})' captures exactly 4 digits, suitable for a year.
Explain how you would use str.extract() to pull out an area code from phone numbers in a pandas Series.
Think about the pattern for area codes like three digits inside parentheses or at the start.
You got /3 concepts.
Describe the difference in output when extracting one group versus multiple groups with str.extract().
Consider how pandas organizes extracted data based on number of groups.
You got /3 concepts.
Practice
(1/5)
1. What does the str.extract method do in pandas when used with a regex pattern?
easy
A. It replaces matching text with a new string.
B. It deletes rows that don't match the pattern.
C. It pulls out matching parts of text into a new DataFrame.
D. It counts how many times the pattern appears.
Solution
Step 1: Understand str.extract purpose
The method is designed to find parts of text that match a regex pattern and extract them.
Step 2: Check what it returns
It returns a new DataFrame with the extracted groups or NaN if no match is found.
Final Answer:
It pulls out matching parts of text into a new DataFrame. -> Option C
Quick Check:
str.extract extracts text parts [OK]
Hint: Remember: extract pulls out text pieces using patterns [OK]
Common Mistakes:
Thinking it deletes rows instead of extracting text
Confusing extract with replace or count methods
Expecting a single string instead of a DataFrame
2. Which of the following is the correct syntax to extract a 3-digit number from a pandas Series df['code'] using str.extract?
easy
A. df['code'].str.extract('\d{3}')
B. df['code'].str.extract(r'(\d{3})')
C. df['code'].str.extract(r'\d{3}')
D. df['code'].str.extract('(\d{3})')
Solution
Step 1: Identify correct regex string format
Regex pattern must be a raw string (r'...') to handle backslashes properly.
Step 2: Check parentheses for capturing group
Parentheses are needed to capture the group to extract.
Final Answer:
df['code'].str.extract(r'(\d{3})') -> Option B
Quick Check:
Use raw string with parentheses for capture [OK]
Hint: Use raw string and parentheses for capture groups [OK]
A. The regex pattern lacks parentheses for capturing groups.
B. The regex pattern is invalid syntax.
C. The DataFrame column name is incorrect.
D. str.extract cannot extract digits.
Solution
Step 1: Check regex pattern for capture groups
str.extract requires parentheses to capture groups; '\d{3}' has none.
Step 2: Understand effect of missing parentheses
Without parentheses, str.extract returns NaN for all rows because no group is captured.
Final Answer:
The regex pattern lacks parentheses for capturing groups. -> Option A
Quick Check:
Parentheses needed to capture groups [OK]
Hint: Always use parentheses to capture groups in str.extract [OK]
Common Mistakes:
Forgetting parentheses around regex group
Assuming str.extract works without capture groups
Confusing str.extract with str.contains
5. You have a DataFrame df with a column 'data' containing strings like 'Order: #12345, Date: 2023-05-10'. You want to extract the order number and date into separate columns using str.extract. Which regex pattern correctly extracts both parts?
hard
A. r'Order: #(\d+), Date: (\d{4}-\d{2}-\d{2})'
B. r'Order: #(\d+), Date: \d{4}-\d{2}-\d{2}'
C. r'Order: #\d+, Date: (\d{4}-\d{2}-\d{2})'
D. r'Order: #(\d+), Date: (\d{4}/\d{2}/\d{2})'
Solution
Step 1: Identify needed capture groups
We need to capture order number digits and date in 'YYYY-MM-DD' format, so two groups are required.
Step 2: Check regex pattern correctness
r'Order: #(\d+), Date: (\d{4}-\d{2}-\d{2})' has two groups: '(\d+)' for order number and '(\d{4}-\d{2}-\d{2})' for date, matching the format exactly.
Final Answer:
r'Order: #(\d+), Date: (\d{4}-\d{2}-\d{2})' -> Option A
Quick Check:
Two capture groups matching order and date format [OK]
Hint: Use parentheses for each part you want to extract [OK]
Common Mistakes:
Missing parentheses around one or both groups
Wrong date format with slashes instead of dashes
Not capturing order number digits
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.