Bird
Raised Fist0
Pandasdata~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the pandas str.replace() method do when applied to a DataFrame column?
easy
A. It sorts the column values in ascending order.
B. It deletes the entire column from the DataFrame.
C. It converts all strings in the column to uppercase.
D. It replaces specified substrings in each string of the column with a new substring.

Solution

  1. Step 1: Understand the purpose of str.replace()

    This method targets each string in a pandas column and looks for a substring to replace.
  2. Step 2: Identify the effect on the column

    It creates a new column or series where the specified substring is replaced by the new substring in every string.
  3. Final Answer:

    It replaces specified substrings in each string of the column with a new substring. -> Option D
  4. Quick Check:

    str.replace() = substring substitution [OK]
Hint: Think: change parts of text in each cell [OK]
Common Mistakes:
  • Confusing replace with deleting columns
  • Thinking it changes case of strings
  • Assuming it sorts data
2. Which of the following is the correct syntax to replace all occurrences of 'cat' with 'dog' in a pandas column named pets?
easy
A. df['pets'].str.replace('cat', 'dog')
B. df['pets'].replace('cat', 'dog')
C. df.str.replace('cat', 'dog')['pets']
D. df['pets'].replace_all('cat', 'dog')

Solution

  1. Step 1: Identify the correct method for string replacement

    In pandas, str.replace() is used on a Series of strings to replace substrings.
  2. Step 2: Check the syntax for applying str.replace() on a column

    The correct syntax is df['column'].str.replace(old, new). df['pets'].str.replace('cat', 'dog') matches this.
  3. Final Answer:

    df['pets'].str.replace('cat', 'dog') -> Option A
  4. Quick Check:

    Use .str.replace() on Series for string substitution [OK]
Hint: Use .str.replace() on the column, not .replace() alone [OK]
Common Mistakes:
  • Using .replace() directly on the DataFrame column
  • Trying to call .str.replace() on the whole DataFrame
  • Using non-existent methods like replace_all
3. Given the DataFrame:
import pandas as pd
df = pd.DataFrame({'fruit': ['apple', 'banana', 'grape', 'pineapple']})
df['fruit_new'] = df['fruit'].str.replace('apple', 'orange')

What is the content of df['fruit_new']?
medium
A. ['orange', 'banana', 'grape', 'pineapple']
B. ['apple', 'banana', 'grape', 'pineapple']
C. ['orange', 'banana', 'grape', 'pineorange']
D. ['orange', 'orange', 'orange', 'orange']

Solution

  1. Step 1: Understand what str.replace('apple', 'orange') does

    It replaces every occurrence of 'apple' in each string with 'orange'.
  2. Step 2: Apply replacement to each value

    'apple' becomes 'orange', 'banana' unchanged, 'grape' unchanged, 'pineapple' contains 'apple' at the end, so it becomes 'pineorange'.
  3. Final Answer:

    ['orange', 'banana', 'grape', 'pineorange'] -> Option C
  4. Quick Check:

    Substring 'apple' replaced wherever found [OK]
Hint: Replace changes substrings inside words too [OK]
Common Mistakes:
  • Assuming only exact matches replaced, not substrings
  • Thinking 'pineapple' stays unchanged
  • Confusing replacement with full string swap
4. What is wrong with this code snippet?
df['col'].str.replace('a', 'b')

It does not change df['col'] as expected.
medium
A. The result is not assigned back to df['col'] to save changes.
B. The method str.replace() does not exist in pandas.
C. The arguments 'a' and 'b' must be integers, not strings.
D. You must use df.replace() instead of str.replace().

Solution

  1. Step 1: Understand that str.replace() returns a new Series

    The method does not change the original column in place; it returns a modified copy.
  2. Step 2: Identify the missing assignment

    To keep changes, assign the result back: df['col'] = df['col'].str.replace('a', 'b').
  3. Final Answer:

    The result is not assigned back to df['col'] to save changes. -> Option A
  4. Quick Check:

    Assign result to keep changes [OK]
Hint: Always assign back to save string replacements [OK]
Common Mistakes:
  • Expecting in-place change without assignment
  • Confusing str.replace() with DataFrame.replace()
  • Thinking str.replace() modifies original data silently
5. You have a DataFrame with a column emails containing email addresses. You want to anonymize the domain part by replacing everything after '@' with 'example.com'. Which code correctly does this?
hard
A. df['emails'].replace(r'@.*', '@example.com')
B. df['emails'].str.replace(r'@.*', '@example.com', regex=True)
C. df['emails'].str.replace('example.com', '@')
D. df['emails'].str.replace('@', '@example.com')

Solution

  1. Step 1: Identify the pattern to replace

    The domain part starts with '@' and continues to the end, so the regex '@.*' matches it.
  2. Step 2: Use str.replace() with regex=True to replace domain

    df['emails'].str.replace(r'@.*', '@example.com', regex=True) uses regex to replace everything after '@' with '@example.com'.
  3. Step 3: Check other options

    A uses Series.replace() which defaults to regex=False, treating '@.*' literally. B replaces only '@' leaving the domain. C replaces in the wrong direction.
  4. Final Answer:

    df['emails'].str.replace(r'@.*', '@example.com', regex=True) -> Option B
  5. Quick Check:

    Use regex in str.replace() for domain substitution [OK]
Hint: Use regex '@.*' with regex=True to replace domain [OK]
Common Mistakes:
  • Not using regex=True for pattern matching
  • Replacing only '@' without domain
  • Using DataFrame.replace instead of str.replace
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