Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas filling nan with previous row value multiplied with another column

Tags:

python

pandas

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.

like image 515
susant karande Avatar asked Jan 24 '23 18:01

susant karande


1 Answers

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
like image 171
BENY Avatar answered Feb 02 '23 00:02

BENY