Bit of a trick using pandas.DataFrame.ffill
with notna
and where
:
df.where(df.ffill().notna(), 0)
Or using pandas.DataFrame.interpolate
:
df.interpolate('zero', fill_value=0, limit_direction='backward')
Output:
A B C0 1 0.0 0.01 2 0.0 5.02 3 3.0 NaN3 4 NaN NaN