Pandas Propagate Data

Question:

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

Answers:

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

df['Position_Price'] =  df['Order_Price'].replace(to_replace=0, method='ffill')
Answered By: Nuri Taş
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.