Nelly Kong
Nelly Kong

Reputation: 289

pandas element wise operation for each column

This should be an easy question but I really couldn't find a similar one.... I'm trying to scale each column of a data frame by subtracting the minimum of each column and divide by the scale. For R, I can do:

apply(df,0,function(x) (x-min(x))/(max(x)-min(x)))

The data frame are integers only:

    col1 col2 col3
1.  20   32   14    
2.  15   33   67
3.  12   14   24

But pandas doesn't seem to do element wise division by default.

Upvotes: 0

Views: 2471

Answers (1)

fuglede
fuglede

Reputation: 18201

Sounds like you want to do something like

In [23]: (df - df.min()) / (df.max() - df.min())
Out[23]:
    col1      col2      col3
1  1.000  0.947368  0.000000
2  0.375  1.000000  1.000000
3  0.000  0.000000  0.188679

Upvotes: 3

Related Questions