pandas dataframe columns scaling with sklearn pandas dataframe columns scaling with sklearn python python

pandas dataframe columns scaling with sklearn


I am not sure if previous versions of pandas prevented this but now the following snippet works perfectly for me and produces exactly what you want without having to use apply

>>> import pandas as pd>>> from sklearn.preprocessing import MinMaxScaler>>> scaler = MinMaxScaler()>>> dfTest = pd.DataFrame({'A':[14.00,90.20,90.95,96.27,91.21],                           'B':[103.02,107.26,110.35,114.23,114.68],                           'C':['big','small','big','small','small']})>>> dfTest[['A', 'B']] = scaler.fit_transform(dfTest[['A', 'B']])>>> dfTest          A         B      C0  0.000000  0.000000    big1  0.926219  0.363636  small2  0.935335  0.628645    big3  1.000000  0.961407  small4  0.938495  1.000000  small


Like this?

dfTest = pd.DataFrame({           'A':[14.00,90.20,90.95,96.27,91.21],           'B':[103.02,107.26,110.35,114.23,114.68],            'C':['big','small','big','small','small']         })dfTest[['A','B']] = dfTest[['A','B']].apply(                           lambda x: MinMaxScaler().fit_transform(x))dfTest    A           B           C0   0.000000    0.000000    big1   0.926219    0.363636    small2   0.935335    0.628645    big3   1.000000    0.961407    small4   0.938495    1.000000    small


df = pd.DataFrame(scale.fit_transform(df.values), columns=df.columns, index=df.index)

This should work without depreciation warnings.