Bird
Raised Fist0
Pandasdata~20 mins

Regex operations in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Regex Mastery in Pandas
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this regex filter in Pandas?
Given the DataFrame df below, what will be the output of df[df['Name'].str.contains('^A.*n$', regex=True)]?
Pandas
import pandas as pd

data = {'Name': ['Alan', 'Ann', 'Aarons', 'Ben', 'Ao']}
df = pd.DataFrame(data)

result = df[df['Name'].str.contains('^A.*n$', regex=True)]
print(result)
A
   Name
0  Alan
1   Ann
B
   Name
0  Alan
1   Ann
2  Aaron
C
   Name
1   Ann
4    An
D
   Name
1   Ann
Attempts:
2 left
💡 Hint
The regex '^A.*n$' means the string starts with 'A' and ends with 'n'.
❓ data_output
intermediate
1:30remaining
How many rows match this regex pattern?
Using the DataFrame df below, how many rows will match df['Email'].str.match(r'^[\w.-]+@example\.com$')?
Pandas
import pandas as pd

data = {'Email': ['user1@example.com', 'user2@test.com', 'admin@example.com', 'guest@example.org']}
df = pd.DataFrame(data)

matches = df['Email'].str.match(r'^[\w.-]+@example\.com$')
print(matches.sum())
A4
B3
C1
D2
Attempts:
2 left
💡 Hint
Look for emails ending exactly with '@example.com'.
🔧 Debug
advanced
2:00remaining
Why does this regex filter raise an error?
Consider this code snippet:
df['Phone'].str.contains('^\d{3}-\d{3}-\d{4}$')
Why does it raise a ValueError?
Pandas
import pandas as pd

data = {'Phone': ['123-456-7890', '987-654-3210', None]}
df = pd.DataFrame(data)

result = df['Phone'].str.contains('^\d{3}-\d{3}-\d{4}$')
ABecause the column contains None values and str.contains does not handle NaNs by default.
BBecause the regex pattern needs raw string notation (r'...').
CBecause the regex pattern is invalid syntax.
DBecause the DataFrame is empty.
Attempts:
2 left
💡 Hint
Check how pandas handles missing values in string operations.
❓ visualization
advanced
2:30remaining
Which plot shows the count of rows matching a regex pattern?
Given this DataFrame df with a 'Category' column, which code snippet produces a bar plot showing counts of rows where 'Category' matches regex '^A.*'?
Pandas
import pandas as pd
import matplotlib.pyplot as plt

data = {'Category': ['Apple', 'Banana', 'Apricot', 'Berry', 'Avocado']}
df = pd.DataFrame(data)
A
df['Category'].value_counts().plot(kind='bar')
plt.show()
B
df['Category'].str.contains('^A.*').plot(kind='bar')
plt.show()
C
df[df['Category'].str.match('^A.*')]['Category'].value_counts().plot(kind='bar')
plt.show()
D
df[df['Category'].str.contains('^A.*')].plot(kind='bar')
plt.show()
Attempts:
2 left
💡 Hint
You want counts of matching categories, not just True/False bars.
🚀 Application
expert
3:00remaining
Extract area codes from phone numbers using regex in Pandas
Given a DataFrame df with a 'Phone' column containing phone numbers like '123-456-7890', which code extracts the area code (first three digits) into a new column 'AreaCode'?
Pandas
import pandas as pd

data = {'Phone': ['123-456-7890', '987-654-3210', '555-123-4567']}
df = pd.DataFrame(data)
Adf['AreaCode'] = df['Phone'].str.extract(r'\d{3}-(\d{3})-\d{4}')
Bdf['AreaCode'] = df['Phone'].str.extract(r'(\d{3})-\d{3}-\d{4}')
Cdf['AreaCode'] = df['Phone'].str.slice(0, 3)
Ddf['AreaCode'] = df['Phone'].str.findall(r'\d{3}').str[1]
Attempts:
2 left
💡 Hint
Use capturing groups in regex to extract parts of strings.

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

  1. 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.
  2. Step 2: Differentiate from other methods

    str.replace changes text, str.extract pulls out matches, and sorting is unrelated.
  3. Final Answer:

    It checks if each string in a column contains the regex pattern and returns true or false. -> Option D
  4. 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

  1. 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.
  2. 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').
  3. Final Answer:

    df['col'].str.replace('\d+', 'X') -> Option A
  4. 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
  • Wrong regex pattern syntax
  • Using str.extract instead of replace
3. Given the DataFrame:
import pandas as pd
df = pd.DataFrame({'text': ['apple123', 'banana', 'cherry456', 'date789']})
df['numbers'] = df['text'].str.extract('(\d+)')

What will be the content of the new column numbers?
medium
A. ['123', 'banana', '456', '789']
B. ['apple', NaN, 'cherry', 'date']
C. ['123', NaN, '456', '789']
D. [NaN, NaN, NaN, NaN]

Solution

  1. Step 1: Understand str.extract with regex

    The regex (\d+) captures one or more digits. str.extract returns the first match or NaN if none.
  2. Step 2: Apply regex to each string

    'apple123' matches '123', 'banana' has no digits so NaN, 'cherry456' matches '456', 'date789' matches '789'.
  3. Final Answer:

    ['123', NaN, '456', '789'] -> Option C
  4. Quick Check:

    Extract digits or NaN if none [OK]
Hint: Extract returns first regex group or NaN if no match [OK]
Common Mistakes:
  • Expecting extract to return full string
  • Confusing extract with contains or replace
  • Not handling NaN for no matches
4. Consider this code snippet:
import pandas as pd
df = pd.DataFrame({'col': ['abc123', 'def456', 'ghi789']})
df['col'].str.match('[a-z]+\d')

What is the error or issue in this code?
medium
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

  1. 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.
  2. 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.
  3. Final Answer:

    The regex pattern misses the last digit, so match returns false for all rows. -> Option A
  4. 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

  1. Step 1: Choose method to check pattern presence

    str.contains returns true/false if regex matches anywhere in string, suitable for validation.
  2. 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.
  3. Final Answer:

    df['valid'] = df['emails'].str.contains(r'^[\w\.-]+@[\w\.-]+\.\w+$') -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard