Challenge - 5 Problems
Replace Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Think about how replace() uses the dictionary to map old values to new ones only where specified.
✗ Incorrect
The replace() method substitutes 'banana' with 'blueberry' and 'date' with 'dragonfruit' in the 'Fruit' column, leaving other values unchanged.
❓ data_output
intermediate1: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'}})
Attempts:
2 left
💡 Hint
Count how many times the values to be replaced appear in the column.
✗ Incorrect
The value 'blue' appears twice and 'yellow' once, so total replacements are 3. But since 'yellow' is replaced once and 'blue' twice, total replaced values are 3.
🔧 Debug
advanced1: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})
Attempts:
2 left
💡 Hint
Check the expected argument types for replace() method.
✗ Incorrect
The replace() method expects the first argument to be the value to replace or a dict mapping. Passing 1 and then a dict causes a TypeError.
🚀 Application
advanced2: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']})
Attempts:
2 left
💡 Hint
Use a dictionary mapping column name to another dictionary of old to new values.
✗ Incorrect
Option A correctly uses a nested dictionary to replace specific values in the 'Status' column with np.nan.
🧠 Conceptual
expert2: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)
Attempts:
2 left
💡 Hint
regex=true allows pattern matching and replacement inside strings.
✗ Incorrect
The regex '\d+' matches digits and replaces them with 'NUM' inside each string in the 'Code' column.