What does the replace() method do in pandas DataFrames?
The replace() method substitutes specified values in a DataFrame or Series with new values. It helps clean or modify data by changing unwanted or incorrect values.
Click to reveal answer
beginner
How do you replace all occurrences of the value 0 with 100 in a pandas DataFrame column named 'score'?
You can use: df['score'] = df['score'].replace(0, 100) This changes every 0 in 'score' to 100.
Click to reveal answer
intermediate
Can replace() handle multiple values at once? How?
Yes! You can pass a dictionary to replace() where keys are old values and values are new ones. For example: df.replace({0: 100, -1: 999}) replaces 0 with 100 and -1 with 999 in the whole DataFrame.
Click to reveal answer
intermediate
What happens if you use replace() on a DataFrame without assigning it back or using inplace=True?
The original DataFrame stays the same because replace() returns a new DataFrame with changes. You must assign it back or use inplace=True to modify the original.
Click to reveal answer
beginner
Is it possible to replace values only in specific columns using replace()?
Yes. You can select columns first, then apply replace(). For example: df['col1'] = df['col1'].replace(old, new) changes values only in 'col1'.
Click to reveal answer
What does df.replace(0, 100) do?
AReplaces 100 with 0
BReplaces all 0 values with 100 in the entire DataFrame
CAdds 100 to all 0 values
DDeletes all rows with 0
✗ Incorrect
The replace() method substitutes all 0 values with 100.
How do you replace multiple values at once in pandas?
AUsing a dictionary mapping old to new values
BUsing a list of values
CUsing a tuple of values
DUsing a string pattern
✗ Incorrect
Passing a dictionary like {old_value: new_value} replaces multiple values.
What must you do to keep changes after using replace()?
ANothing, changes are automatic
BCall <code>save()</code> method
CAssign the result back or use <code>inplace=True</code>
DRestart the kernel
✗ Incorrect
By default, replace() returns a new object; assign it or use inplace=True to modify original.
Can replace() be used on a single column?
AOnly on string columns
BNo, only on entire DataFrames
COnly on numeric columns
DYes, by selecting the column first
✗ Incorrect
You select the column first, then apply replace() to it.
Which of these is a correct way to replace -1 with NaN in pandas?
Adf.replace(-1, np.nan)
Bdf.replace(-1, None)
Cdf.replace(-1, 'NaN')
Ddf.replace(-1, 0)
✗ Incorrect
Use np.nan to represent missing values in pandas.
Explain how to use replace() to change multiple values in a DataFrame.
Think about passing a map of old to new values.
You got /4 concepts.
Describe what happens if you call replace() without assignment or inplace=True.
Consider how pandas methods handle changes by default.
You got /3 concepts.
Practice
(1/5)
1. What does the replace() function do in data analysis with pandas?
easy
A. It sorts the DataFrame based on column values.
B. It deletes rows containing specific values.
C. It substitutes specified values in a DataFrame or Series with new values.
D. It calculates the mean of numeric columns.
Solution
Step 1: Understand the purpose of replace()
The replace() function is used to change specific values in data, not to delete or sort data.
Step 2: Match function to description
Replacing means substituting old values with new ones, which matches It substitutes specified values in a DataFrame or Series with new values.
Final Answer:
It substitutes specified values in a DataFrame or Series with new values. -> Option C
Quick Check:
replace() = substitute values [OK]
Hint: Remember: replace() swaps old values with new ones [OK]
Common Mistakes:
Confusing replace() with drop() or sort()
Thinking replace() deletes data
Assuming replace() calculates statistics
2. Which of the following is the correct syntax to replace all occurrences of 0 with 100 in a pandas Series named data?
easy
A. data.replace(0, 100)
B. data.replace(0 => 100)
C. data.replace(0: 100)
D. data.replace(0 == 100)
Solution
Step 1: Recall correct replace() syntax
The correct syntax uses a comma to separate old and new values: replace(old_value, new_value).
Step 2: Check each option
Only data.replace(0, 100) uses the correct syntax. Others use invalid operators like =>, :, or ==.
Final Answer:
data.replace(0, 100) -> Option A
Quick Check:
replace(old, new) uses comma [OK]
Hint: Use comma between old and new values in replace() [OK]
Common Mistakes:
Using arrows or colons instead of commas
Confusing replace() with comparison operators
Missing parentheses or commas
3. Given the pandas Series data = pd.Series(['apple', 'banana', 'apple', 'cherry']), what is the output of data.replace('apple', 'orange')?
Hint: Only matching values change, others stay same [OK]
Common Mistakes:
Replacing all values instead of only matches
Confusing replace() with map() or apply()
Expecting in-place change without assignment
4. What is wrong with this code snippet if we want to replace all 1s with 10s in a pandas DataFrame column df['col']?
df['col'].replace(1, 10)
print(df['col'])
medium
A. The print statement should be before replace().
B. The replace() method does not modify df['col'] unless assigned or inplace=True.
C. replace() cannot be used on DataFrame columns.
D. The syntax of replace() is incorrect; it needs a dictionary.
Solution
Step 1: Understand replace() behavior
By default, replace() returns a new object and does not change the original unless assigned or inplace=True.
Step 2: Identify the issue in code
The code calls replace() but does not assign the result back, so df['col'] remains unchanged.
Final Answer:
The replace() method does not modify df['col'] unless assigned or inplace=True. -> Option B
Quick Check:
Assign or use inplace=True to keep changes [OK]
Hint: Always assign replace() result or use inplace=True [OK]
Common Mistakes:
Expecting replace() to change data without assignment
Using wrong syntax for replace()
Thinking replace() works only on entire DataFrames
5. You have a DataFrame df with a column 'status' containing values: 'old', 'new', 'old', 'pending'. You want to replace 'old' with 'archived' and 'pending' with 'waiting' in one command. Which code correctly does this?
hard
A. df['status'].replace(['old', 'pending'], ['archived', 'waiting'])
B. df['status'].replace('old' => 'archived', 'pending' => 'waiting')
C. df['status'].replace('old', 'archived').replace('pending', 'waiting')
D. df['status'].replace({'old': 'archived', 'pending': 'waiting'}, inplace=True)
Solution
Step 1: Identify multi-value replacement syntax
To replace multiple values at once, use a dictionary mapping old to new values.
Step 2: Check for inplace modification
Using inplace=True applies changes directly to the DataFrame column.
Step 3: Evaluate options
df['status'].replace({'old': 'archived', 'pending': 'waiting'}, inplace=True) uses dictionary and inplace=True correctly. df['status'].replace(['old', 'pending'], ['archived', 'waiting']) returns new Series but does not assign. df['status'].replace('old', 'archived').replace('pending', 'waiting') chains replace but is less efficient. df['status'].replace('old' => 'archived', 'pending' => 'waiting') uses invalid syntax.
Final Answer:
df['status'].replace({'old': 'archived', 'pending': 'waiting'}, inplace=True) -> Option D
Quick Check:
Use dict + inplace=True for multiple replacements [OK]
Hint: Use dict and inplace=True for multiple replacements [OK]
Common Mistakes:
Using list replacement without assignment
Chaining replace() without assignment
Using invalid syntax with arrows
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.