Recall & Review
beginner
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: <br>
df['score'] = df['score'].replace(0, 100)<br>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:<br>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:<br>df['col1'] = df['col1'].replace(old, new) changes values only in 'col1'.Click to reveal answer
What does
df.replace(0, 100) do?✗ Incorrect
The
replace() method substitutes all 0 values with 100.How do you replace multiple values at once in pandas?
✗ Incorrect
Passing a dictionary like
{old_value: new_value} replaces multiple values.What must you do to keep changes after using
replace()?✗ 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?✗ 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?
✗ 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.