In [2]:
df
Out[2]:
A B C
0 0 0 a
1 0 1 b
2 1 2 c
3 1 3 d
4 2 4 e
5 2 5 f

Replace a value in a column

In [3]:
df['A'] = df['A'].replace(1, 100)

df
Out[3]:
A B C
0 0 0 a
1 0 1 b
2 100 2 c
3 100 3 d
4 2 4 e
5 2 5 f
In [4]:
df = pd.DataFrame(default_dict)

Replace a value in specific columns

In [5]:
df.replace({"A":1,"B":1}, 100)
Out[5]:
A B C
0 0 0 a
1 0 100 b
2 100 2 c
3 100 3 d
4 2 4 e
5 2 5 f

Replace a value in all columns

In [6]:
df.replace(1, 100)
Out[6]:
A B C
0 0 0 a
1 0 100 b
2 100 2 c
3 100 3 d
4 2 4 e
5 2 5 f

Use ffill to replace values with the value above them in a column.

0 is not replace in column A as there is not a valid value above the value.

In [7]:
df.replace([0, 2], method='ffill')
Out[7]:
A B C
0 0 0 a
1 0 1 b
2 1 1 c
3 1 3 d
4 1 4 e
5 1 5 f

Use bfill to replace values with the value below them in a column.

2 is not replace in column A as there is not a valid value below the value.

In [8]:
df.replace([0, 2], method='bfill')
Out[8]:
A B C
0 1 1 a
1 1 1 b
2 1 3 c
3 1 3 d
4 2 4 e
5 2 5 f