Home > Mobile >  Forward Fill NaN's, Incrementing by 1 of the Previous Non-NaN Value
Forward Fill NaN's, Incrementing by 1 of the Previous Non-NaN Value

Time:10-09

I want to forward fill nan values in a column, by adding 1 to the previous non-nan value as you go down the column.

Here is a sample code to run and compare to:

from pandas import DataFrame
from numpy import nan

# This is the dataframe I'm looking for in the end
desired_df = DataFrame.from_dict({'col':[nan, nan, 0, 1, 2, 3, 4, 0, 1, 2, 0, 0, 1, 2, 3, 4, 5, 6, 0, 1]})

# This is the input dataframe
df = DataFrame.from_dict({'col':[nan, nan, 0, nan, nan, nan, nan, 0, nan, nan, 0, 0, nan, nan, nan, nan, nan, nan, 0, nan]})

####### Turn "df" into "desired_df" here #######

# Check if they match!
assert df.merge(desired_df).shape == df.shape, "You suck!"

IDEALLY WITHOUT A FOR LOOP, but it's not crucial.

CodePudding user response:

I'd suggest something like this:

missing = df.isna()
missing_cumsum = missing.cumsum()
offset = missing_cumsum - missing_cumsum.where(~missing).ffill().fillna(0)
df = df.ffill()   offset

I based this on code from this answer.

  • Related