Examples
Scalar to_replace
and value
import numpy as np
import pandas as pd
s = pd.Series([0, 2, 3, 4, 5])
s.replace(0, 6)
df = pd.DataFrame({'P': [0, 2, 3, 4, 5],
'Q': [6, 7, 8, 9, 10],
'R': ['p', 'q', 'r', 's', 't']})
df.replace(0, 6)
List-like to_replace
df.replace([0, 2, 3, 4], 5)
df.replace([0, 2, 3, 4], [5, 4, 3, 2])
s.replace([2, 3], method='bfill')
dict-like to_replace
df.replace({0: 20, 2: 200})
df.replace({'P': 0, 'Q': 6}, 100)
df.replace({'P': {0: 200, 4: 400}})
Regular expression to_replace
df = pd.DataFrame({'P': ['bar', 'ffa', 'bfg'],
'Q': ['mno', 'bat', 'xyz']})
df.replace(to_replace=r'^ba.$', value='new', regex=True)
df.replace({'P': r'^ba.$'}, {'P': 'new'}, regex=True)
df.replace(regex=r'^ba.$', value='new')
df.replace(regex={r'^ba.$': 'new', 'ffa': 'xyz'})
df.replace(regex=[r'^ba.$', 'ffa'], value='new')
Note that when replacing multiple bool or datetime64 objects, the data types in the to_replace
parameter must match the data type of the value being replaced:
s = pd.Series([10, 'p', 'p', 'q', 'p'])
When one uses a dict as the to_replace value, it is like the value(s) in the dict are equal
to the value parameter. s.replace({'p': None}) is equivalent to s.replace(to_replace={'p': None},
value=None, method=None):
s.replace({'p': None})
When value=None and to_replace is a scalar, list or tuple, replace uses the method parameter
(default ‘pad’) to do the replacement. So this is why the ‘p’ values are being replaced by 10 in
rows 1 and 2 and ‘q’ in row 4 in this case. The command s.replace('p', None) is actually equivalent
to s.replace(to_replace='p', value=None, method='pad'):
s.replace('p', None)