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

Question:

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.

Asked By: wildcat89

||

Answers:

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.

Answered By: Nick ODell
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.