0
0
Data Analysis Pythondata~20 mins

replace() for value substitution in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Replace Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of replace() on a pandas DataFrame column
What is the output of the following code snippet?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'Fruit': ['apple', 'banana', 'cherry', 'banana', 'apple']})
df['Fruit'] = df['Fruit'].replace('banana', 'orange')
print(df)
A
     Fruit
0    apple
1   orange
2   cherry
3   orange
4    apple
B
     Fruit
0    apple
1   banana
2   cherry
3   banana
4    apple
C
     Fruit
0    apple
1   orange
2   cherry
3   banana
4    apple
D
     Fruit
0    apple
1   orange
2   orange
3   orange
4    apple
Attempts:
2 left
💡 Hint
replace() changes all exact matches of the old value with the new value in the column.
data_output
intermediate
1:30remaining
Number of replacements made by replace()
Given this DataFrame and code, how many values are replaced?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'Color': ['red', 'blue', 'green', 'blue', 'yellow']})
replaced_df = df.replace({'blue': 'cyan'})
count_replaced = (df['Color'] != replaced_df['Color']).sum()
print(count_replaced)
A3
B1
C2
D0
Attempts:
2 left
💡 Hint
Count how many 'blue' values are in the original column.
🔧 Debug
advanced
2:00remaining
Identify the error in replace() usage
What error does this code raise?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'Animal': ['cat', 'dog', 'bird']})
df['Animal'] = df['Animal'].replace(['cat', 'dog'], 'fish', inplace=True)
print(df)
AAttributeError: 'NoneType' object has no attribute 'replace'
BTypeError: replace() got an unexpected keyword argument 'inplace'
CValueError: Length of replacement does not match length of values
DNo error, prints DataFrame with 'cat' and 'dog' replaced by 'fish'
Attempts:
2 left
💡 Hint
Check what replace() returns when inplace=True is used.
🚀 Application
advanced
2:00remaining
Replacing multiple values with a dictionary
Which option correctly replaces 'NY' with 'New York' and 'CA' with 'California' in the 'State' column?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'State': ['NY', 'CA', 'TX', 'NY', 'FL']})

# Replace code here
print(df)
Adf['State'] = df['State'].replace({'NY': 'California', 'CA': 'New York'})
Bdf['State'].replace(['NY', 'CA'], ['New York', 'California'])
Cdf['State'] = df['State'].replace(['NY', 'CA'], ['New York', 'California'])
Ddf['State'] = df['State'].replace({'NY': 'New York', 'CA': 'California'})
Attempts:
2 left
💡 Hint
Use a dictionary to map old values to new values correctly.
🧠 Conceptual
expert
2:30remaining
Understanding replace() behavior with regex
What is the output of this code?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'Code': ['A1', 'B2', 'C3', 'A4']})
df['Code'] = df['Code'].replace(to_replace='A.', value='Z', regex=True)
print(df)
A
  Code
0   Z1
1   B2
2   C3
3   Z4
B
  Code
0    Z
1   B2
2   C3
3    Z
C
  Code
0   A1
1   B2
2   C3
3   A4
D
  Code
0   Z
1   Z
2   Z
3   Z
Attempts:
2 left
💡 Hint
The regex 'A.' matches any string starting with 'A' followed by any character.