Normalizing a pandas DataFrame by row

ChrisB picture ChrisB · Sep 3, 2013 · Viewed 22.8k times · Source

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

Answer

joris picture joris · Sep 3, 2013

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

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

See pandas User Guide: Matching / broadcasting behavior