df
0 is not replaced in column A as there is not a valid value above the value.
df.replace([0, 2], method='ffill')
2 is not replaced in column A as there is not a valid value below the value.
df.replace([0, 2], method='bfill')