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()).T
Pandas broadcasting rules prevent df / df.sum(axis=1) from doing this
df / df.sum(axis=1)
To overcome the broadcasting issue, you can use the div method:
div
df.div(df.sum(axis=1), axis=0)
See pandas User Guide: Matching / broadcasting behavior
2.1m questions
2.1m answers
60 comments
57.0k users