0
0
Pandasdata~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Replace Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of replace() with dictionary mapping
What is the output of the following code snippet using pandas replace() method with a dictionary for value substitution?
Pandas
import pandas as pd

df = pd.DataFrame({'Fruit': ['apple', 'banana', 'cherry', 'date']})
result = df.replace({'Fruit': {'banana': 'blueberry', 'date': 'dragonfruit'}})
print(result)
A{'Fruit': ['apple', 'blueberry', 'cherry', 'dragonfruit']}
B{'Fruit': ['apple', 'banana', 'cherry', 'date']}
CKeyError
D{'Fruit': ['blueberry', 'banana', 'dragonfruit', 'date']}
Attempts:
2 left
💡 Hint
Think about how replace() uses the dictionary to map old values to new ones only where specified.
data_output
intermediate
1:30remaining
Number of replaced values in DataFrame
After running the code below, how many values in the DataFrame will be replaced?
Pandas
import pandas as pd

df = pd.DataFrame({'Color': ['red', 'blue', 'green', 'blue', 'yellow']})
new_df = df.replace({'Color': {'blue': 'cyan', 'yellow': 'magenta'}})
A2
B3
C1
D0
Attempts:
2 left
💡 Hint
Count how many times the values to be replaced appear in the column.
🔧 Debug
advanced
1:30remaining
Identify the error in replace() usage
What error will this code raise when trying to replace values in a pandas DataFrame?
Pandas
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3]})
# Incorrect replace usage
result = df.replace(1, {2: 20})
AKeyError
BValueError
CTypeError
DNo error, runs successfully
Attempts:
2 left
💡 Hint
Check the expected argument types for replace() method.
🚀 Application
advanced
2:00remaining
Using replace() to clean categorical data
You have a DataFrame with a column 'Status' containing values 'yes', 'no', 'n/a', and 'unknown'. You want to replace 'n/a' and 'unknown' with NaN for analysis. Which code snippet correctly does this?
Pandas
import pandas as pd
import numpy as np

df = pd.DataFrame({'Status': ['yes', 'no', 'n/a', 'unknown', 'yes']})
Adf.replace({'Status': {'n/a': np.nan, 'unknown': np.nan}})
Bdf.replace({'Status': ['n/a', 'unknown'], np.nan})
Cdf.replace(['n/a', 'unknown'], np.nan, inplace=True)
Ddf.replace({'n/a': np.nan, 'unknown': np.nan})
Attempts:
2 left
💡 Hint
Use a dictionary mapping column name to another dictionary of old to new values.
🧠 Conceptual
expert
2:30remaining
Behavior of replace() with regex=true
Given the DataFrame below, what will be the output after applying replace() with regex=true?
Pandas
import pandas as pd

df = pd.DataFrame({'Code': ['abc123', 'def456', 'ghi789']})
result = df.replace({'Code': r'\d+'}, 'NUM', regex=True)
print(result)
A{'Code': ['NUM123', 'NUM456', 'NUM789']}
BTypeError
C{'Code': ['abc123', 'def456', 'ghi789']}
D{'Code': ['abcNUM', 'defNUM', 'ghiNUM']}
Attempts:
2 left
💡 Hint
regex=true allows pattern matching and replacement inside strings.