47

I'd like to scale some (but not all) of the columns in a Pandas dataFrame using a MinMaxScaler. How can I do it?

lte__
  • 7,175
  • 25
  • 74
  • 131

2 Answers2

56

Demo:

In [90]: df = pd.DataFrame(np.random.randn(5, 3), index=list('abcde'), columns=list('xyz'))

In [91]: df
Out[91]:
          x         y         z
a -0.325882 -0.299432 -0.182373
b -0.833546 -0.472082  1.158938
c -0.328513 -0.664035  0.789414
d -0.031630 -1.040802 -1.553518
e  0.813328  0.076450  0.022122

In [92]: from sklearn.preprocessing import MinMaxScaler

In [93]: mms = MinMaxScaler()

In [94]: df[['x','z']] = mms.fit_transform(df[['x','z']])

In [95]: df
Out[95]:
          x         y         z
a  0.308259 -0.299432  0.505500
b  0.000000 -0.472082  1.000000
c  0.306662 -0.664035  0.863768
d  0.486932 -1.040802  0.000000
e  1.000000  0.076450  0.580891

the same result can be also achieved using sklearn.preprocessing.minmax_scale:

from sklearn.preprocessing import minmax_scale

df[['x','z']] = minmax_scale(df[['x','z']])
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
  • 8
    It might be helpful to some to point out that `minmax_scale` works on single dataframe columns out of the box, where `MinMaxScaler` seems to require multiple columns. If you wanted to scale only `x`, `df['x'] = minmax_scale(df['x'])`. If your values being scaled aren't float, then `df['x'] = minmax_scale(df['x'].astype(np.float64))`, to avoid a dtype conversion warning. – Julian Drago May 09 '19 at 05:20
26

Since sklearn >= 0.20 you can do it using Column Transformer

standard_transformer = Pipeline(steps=[
        ('standard', StandardScaler())])

minmax_transformer = Pipeline(steps=[
        ('minmax', MinMaxScaler())])


preprocessor = ColumnTransformer(
        remainder='passthrough', #passthough features not listed
        transformers=[
            ('std', standard_transformer , ['z']),
            ('mm', minmax_transformer , ['x','y'])
        ])
Random
  • 4,519
  • 2
  • 38
  • 46