Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to scale all columns except certain ones in pandas dataframe?

With the following example code all columns are scaled with MinMaxScaler.
How to change in order to only scale column A and column C? Ideally I want to do it by excluding column B by name.

import pandas as pd
from sklearn.preprocessing import MinMaxScaler


scaler = MinMaxScaler()

df = 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':[3,5,4,2,3]})

df[df.columns] = scaler.fit_transform(df[df.columns])

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Take a look at pandas.Index.difference:

scaler.fit_transform(df[df.columns.difference(['B'])])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading