0
0
Pandasdata~20 mins

str.replace() for substitution in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
StrReplace Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of str.replace() with regex in pandas
What is the output of the following code snippet?
Pandas
import pandas as pd

df = pd.DataFrame({'text': ['apple123', 'banana456', 'cherry789']})
df['cleaned'] = df['text'].str.replace(r'\d+', '', regex=True)
print(df['cleaned'].tolist())
A['apple', 'banana', 'cherry']
B['apple123', 'banana456', 'cherry789']
C['123', '456', '789']
D['apple ', 'banana ', 'cherry ']
Attempts:
2 left
💡 Hint
The regex '\d+' matches digits and str.replace removes them.
data_output
intermediate
2:00remaining
Resulting DataFrame after str.replace() substitution
Given the DataFrame below, what is the resulting DataFrame after applying the str.replace() method?
Pandas
import pandas as pd

df = pd.DataFrame({'city': ['New York', 'Los Angeles', 'San Francisco']})
df['city'] = df['city'].str.replace(' ', '_')
print(df)
A
          city
0      NewYork
1   LosAngeles
2  SanFrancisco
B
          city
0      New_York
1   Los_Angeles
2  San_Francisco
C
          city
0      New-York
1   Los-Angeles
2  San-Francisco
D
          city
0      New York
1   Los Angeles
2  San Francisco
Attempts:
2 left
💡 Hint
str.replace(' ', '_') replaces spaces with underscores.
🔧 Debug
advanced
2:00remaining
Identify the error in str.replace() usage
What error will this code raise and why?
Pandas
import pandas as pd

df = pd.DataFrame({'code': ['A1', 'B2', 'C3']})
df['code'] = df['code'].str.replace('\d+', '')
print(df)
ATypeError: replace() missing 1 required positional argument: 'repl'
BValueError: regex pattern not provided
CSyntaxError due to missing raw string prefix
DNo error, output is ['A', 'B', 'C']
Attempts:
2 left
💡 Hint
Check the parameters passed to str.replace() in pandas.
🚀 Application
advanced
2:00remaining
Using str.replace() to clean phone numbers
You have a DataFrame column with phone numbers in various formats. Which code snippet correctly removes all non-digit characters to keep only numbers?
Pandas
import pandas as pd

df = pd.DataFrame({'phone': ['(123) 456-7890', '987.654.3210', '+1 800 555 1234']})
Adf['phone_clean'] = df['phone'].str.replace(r'[^0-9]', '')
Bdf['phone_clean'] = df['phone'].str.replace(r'\d', '', regex=True)
Cdf['phone_clean'] = df['phone'].str.replace(r'\D', '', regex=True)
Ddf['phone_clean'] = df['phone'].str.replace(r'\s', '')
Attempts:
2 left
💡 Hint
Use regex to remove all non-digit characters.
🧠 Conceptual
expert
2:00remaining
Behavior of str.replace() with overlapping patterns
Consider the string 'aaaa'. What is the result of applying str.replace('aa', 'b') once in pandas with regex=False?
Pandas
import pandas as pd

s = pd.Series(['aaaa'])
result = s.str.replace('aa', 'b', regex=False)
print(result[0])
A'bb'
B'aaaa'
C'ab'
D'ba'
Attempts:
2 left
💡 Hint
str.replace replaces non-overlapping occurrences from left to right.