Home > other >  Pandas Propagate Data
Pandas Propagate Data

Time:08-07

newbie here. Working whith Pandas

I have this df

SPT.ant Dif_Sig Order_Price
NaN True Nan
19297 True 19297
19297 False 0
19302 False 0
19350 True 19350
19345 False 0
19355 False 0
19360 False 0

The way "Order_Price" is calculated is : df['Order_Price'] = df['SPT.ant'] * df['Dif_Sig']

and need to propagate de value of "Order_Price" from row 2 "19297" and row 5 "19350" utill a new value is diferent to 0

so the table would result

SPT.ant Dif_Sig Order_Price
NaN True Nan
19297 True 19297
19297 False 19297
19302 False 19297
19350 True 19350
19345 False 19350
19355 False 19350
19360 False 19350

it would be ok to have this in another column as well, whatever is easier.

SPT.ant Dif_Sig Order_Price Position_Price
NaN True Nan Nan
19297 True 19297 19297
19297 False 0 19297
19302 False 0 19297
19350 True 19350 19350
19345 False 0 19350
19355 False 0 19350
19360 False 0 19350

I would like to input the values without having to iterate de df but dont know if posible. I tried many ways but got even more confused now.

Any help would apreciate

CodePudding user response:

You basically need to forward fill the zeros (ffill method)

df['Position_Price'] =  df['Order_Price'].replace(to_replace=0, method='ffill')
  • Related