Python: Scaling numbers column by column with pandas

This is how you can do it using sklearn and the preprocessing module. Sci-Kit Learn has many pre-processing functions for scaling and centering data.

In [0]: from sklearn.preprocessing import MinMaxScaler

In [1]: df = pd.DataFrame({'A':[14,90,90,96,91],
                           'B':[103,107,110,114,114]}).astype(float)

In [2]: df
Out[2]:
    A    B
0  14  103
1  90  107
2  90  110
3  96  114
4  91  114

In [3]: scaler = MinMaxScaler()

In [4]: df_scaled = pd.DataFrame(scaler.fit_transform(df), columns=df.columns)

In [5]: df_scaled
Out[5]:
          A         B
0  0.000000  0.000000
1  0.926829  0.363636
2  0.926829  0.636364
3  1.000000  1.000000
4  0.939024  1.000000

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)