Friedrich Franz Friedrich Franz - 1 year ago 133
Python Question

pandas ffill/bfill for specific amount of observation

I have the following dataframe:

id indicator
1 NaN
1 NaN
1 1
1 NaN
1 NaN
1 NaN


In reality, I have several more ids. My question now is, how do I do a forward or backward fill for a specific range, e.g. for only the next/last 2 observations. My dataframe should look like this:

id indicator
1 NaN
1 NaN
1 1
1 1
1 1
1 NaN


I know the command

df.groupby("id")["indicator"].fillna(value=None, method="ffill")


However, this fills all the missing values instead of just the next two observations. Anyone knows a solution?

Answer Source

almost there, straight from the doc

If method is specified, this is the maximum number of consecutive NaN values to forward/backward fill. In other words, if there is a gap with more than this number of consecutive NaNs, it will only be partially filled. If method is not specified, this is the maximum number of entries along the entire axis where NaNs will be filled. Must be greater than 0 if not None.

df.groupby("id")["indicator"].fillna(value=None,method="ffill",limit=3) 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download