Normalizing a pandas DataFrame by row

前端 未结 2 2030
再見小時候
再見小時候 2020-12-08 09:32

What is the most idiomatic way to normalize each row of a pandas DataFrame? Normalizing the columns is easy, so one (very ugly!) option is:

(df.T / df.T.sum(         


        
2条回答
  •  星月不相逢
    2020-12-08 10:15

    To overcome the broadcasting issue, you can use the div method:

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

    See http://pandas.pydata.org/pandas-docs/stable/basics.html#matching-broadcasting-behavior

提交回复
热议问题