Replacing values in a DataFrame in Pandas
Start your free 7-days trial now!
To replace values in a Pandas DataFrame, use the DataFrame's replace(~) method.
Consider the following DataFrame:
df
A B0 2 21 3 a
Replacing a value in entire DataFrame
To replace all 2s with "b":
A B0 b b1 3 a
Note that replacement is not done in-place, meaning a new DataFrame is returned and the original df is left intact. We can modify df directly by passing in inplace=True.
Replacing a value in specific columns
To replace 2s in just column A with "b":
A B0 b 21 3 a
Notice how the 2 in column B was not replaced.
Replacing multiple values
To replace multiple values, supply them as a list like so:
A B0 b b1 3 b
Here, we are replacing the values 2 and "a" with "b".
Replacing values based on condition
Consider the following DataFrame:
df
A B0 2 41 3 5
To replace values that are greater than 3 with 10:
df[df > 3] = 10df
A B0 2 101 3 10
Here, we are first creating a boolean mask (DataFrame) where True indicates the values that satisfy the condition:
df > 3
A B0 False True1 False True
We then fetch a reference to all the entries in df that correspond to True to in the mask:
df[df > 3]
A B0 NaN 41 NaN 5
Finally, performing assignment will update the non-NaN values:
df[df > 3] = 10df
A B0 2 101 3 10