Search code examples
pythonpython-3.xpandasdataframeimputation

Filling time series column values with last known value


I have a Pandas time series dataframe like this:

id .. .. ..(some cols) 1/1/20 1/2/20 1/3/20 1/4/20 1/5/20 1/6/20 ....
1                      10     20     0      40     0      50
2                      10     30     30     0      0      50
.
.

I want to ffill the 0s in the columns with the last known value to get something like this:

id .. .. ..(some cols) 1/1/20 1/2/20 1/3/20 1/4/20 1/5/20 1/6/20 ....
1                      10     20     20     40     40      50
2                      10     30     30     30     30      50
.
.

Assuming there are some other columns between id and the time series columns, how do I ffill the dataframe like this? I know something like df.ffill(axis = 1) works for Null values, but I wasn't able to find anything to modify it to work with 0.


Solution

  • You can do ffill with mask and update

    df.update(df.filter(like='/').mask(lambda x : x==0).ffill(1))