Getting NaN for dividing each row value by row sum

I’m trying something very simple, I got a Dataframe made of 1 and 0. I’m trying to divide each value in row by the sum of the row, so it will be weight so that the row sums to 1

trading_signal sample

             btc.  eth
2021/08/25.  1.    0
2021/08/26.  1.    1
2021/08/27.  0.    0 

position expected output

             btc.  eth
2021/08/25.  1     0
2021/08/26.  0.5   0.5
2021/08/27.  0     0 

I imagine it to just

positions = trading_signals / trading_signals.sum(axis=1)

But the positions df just populated with NaNs

Answer

You need to divide on axis=0, which is not the default with /. Use div instead:

df.div(df.sum(axis=1), axis=0)

NB. division by 0 will give you NaNs, so add .fillna(0) to fill with 0