I have a csv like the following:

I want to replace any 0 in the first row with the next non-0 value in that column - eg. in the above the first row in the 'momentum_ao' should change to 0.1439166667, the first first row of the 'others_dlr' column should be 0.9153612171 etc (the second row and lower will be unchanged). How should this be done in pandas? This csv has dozens of columns, so I'd want a solution that doesn't involve manually typing in and iterating through columns one-by-one.
This should get the job done. Just fill the zeroes with np.nan values, backfill, and take the first row and reassign it to the original dataframe.
df.iloc[0, :] = df.replace(0, np.nan).bfill().iloc[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