简体   繁体   中英

pandas filling nan with previous row value multiplied with another column

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM