In [1]:
import pandas as pd

In [4]:
df = pd.DataFrame({
    'name':['john','mary','paul'],
    'age':[30,25,40],
    'city':['new york','los angeles','london']
})
df


Out[4]:
age city name
0 30 new york john
1 25 los angeles mary
2 40 london paul

simplest possible example: replace one value with another


In [6]:
df.replace([25],40)


Out[6]:
age city name
0 30 new york john
1 40 los angeles mary
2 40 london paul

replace with dict


In [8]:
df.replace({
    25:26,
    'john':'johnny'
})


Out[8]:
age city name
0 30 new york johnny
1 26 los angeles mary
2 40 london paul

with regex


In [11]:
df.replace('jo.+','FOO',regex=True)


Out[11]:
age city name
0 30 new york FOO
1 25 los angeles mary
2 40 london paul

replace in column


In [18]:
df = pd.DataFrame({
    'name':['john','mary','paul'],
    'num_children':[0,4,5],
    'num_pets':[0,1,2]
})
df


Out[18]:
name num_children num_pets
0 john 0 0
1 mary 4 1
2 paul 5 2

In [19]:
df.replace({'num_pets':{0:1}})


Out[19]:
name num_children num_pets
0 john 0 1
1 mary 4 1
2 paul 5 2

In [ ]: