I have dataframe for which I want to fill nan with values from previous rows mulitplied with pct_change column
    col_to_fill       pct_change
0       1                NaN
1       2                1.0
2       10               0.5
3       nan              0.5
4       nan              1.3
5       nan              2
6       5                3
so for 3rd row 10*0.5 = 5 and use that filled value to fill next rows if its nan.
    col_to_fill        pct_change
0       1                NaN
1       2                1.0
2       10               0.5
3       5                0.5
4       6.5              1.3
5       13               2
6       5                3
I have used this
while df['col_to_fill'].isna().sum() > 0:
    df.loc[df['col_to_fill'].isna(), 'col_to_fill'] = df['col_to_fill'].shift(1) * df['pct_change']
but Its taking too much time as its only filling those row whos previous row are nonnan in one loop.
Try with cumprod after ffill
s = df.col_to_fill.ffill()*df.loc[df.col_to_fill.isna(),'pct_change'].cumprod()
df.col_to_fill.fillna(s, inplace=True)
df
Out[90]: 
   col_to_fill  pct_change
0          1.0         NaN
1          2.0         1.0
2         10.0         0.5
3          5.0         0.5
4          6.5         1.3
5         13.0         2.0
6          5.0         3.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With