Search code examples
pandasscikit-learnrescale

SKLearn MinMaxScaler - scale specific columns only


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


Solution

  • 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'])
            ])