Backwards Fill Dataframe Column Where Limit Of Rows Filled Is Based On Value Of Cell, Perhaps With Bfill() And Limit=x
I have a dataframe that looks like this: import pandas as pd, numpy as np df = pd.DataFrame({'Fill' : [0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 1]}) df['flag'] = (df['Fill'] > 0) df = df.r
Solution 1:
You can create groups for each missing and last non missing values and replace by last values in custom function, if-else
is necessaary for avoid error ValueError: Limit must be greater than 0
:
m = df['Fill'].notnull() & df.flag
g = m.iloc[::-1].cumsum().iloc[::-1]
f = lambda x: x.bfill(limit=int(x.iat[-1]-1)) if x.iat[-1] > 1 else x
df['Fill'] = df.groupby(g)['Fill'].apply(f)
print (df)
Fill flag
0 NaN False
1 3.0 False
2 3.0 False
3 3.0 True
4 NaN False
5 NaN False
6 2.0 False
7 2.0 True
8 NaN False
9 NaN False
10 1.0 True
Post a Comment for "Backwards Fill Dataframe Column Where Limit Of Rows Filled Is Based On Value Of Cell, Perhaps With Bfill() And Limit=x"