Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dataframe-Normalize each row by row's maximum

Is there any convenient way to normalize each row by row's maximum (divide by row's max) eg:

df=
A  B  C
2  1  1
1  4  1
0  2  1
return:
A     B     C
1    0.5   0.5
0.25  1   0.25
0     1    0.5
like image 509
Robin1988 Avatar asked Dec 09 '15 16:12

Robin1988


1 Answers

You can use apply and apply a lambda row-wise:

In [199]:
df.apply(lambda x: x/x.max(), axis=1)

Out[199]:
      A    B     C
0  1.00  0.5  0.50
1  0.25  1.0  0.25
2  0.00  1.0  0.50

You can also use div:

In [205]:
df.div(df.max(axis=1), axis=0)

Out[205]:
      A    B     C
0  1.00  0.5  0.50
1  0.25  1.0  0.25
2  0.00  1.0  0.50
like image 129
EdChum Avatar answered Oct 25 '22 15:10

EdChum