0

I am using code below to normalize columns but it tries to and starts with my label columns, is there anyway to only normalize certain columns?

x = df.values #returns a numpy array
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x)
df = pd.DataFrame(x_scaled)

Thanks

2 Answers 2

1

You can do

df[[col1, col2]] = scaler.fit_transform(df[[col1, col2]])

More details here: pandas dataframe columns scaling with sklearn

Sign up to request clarification or add additional context in comments.

Comments

1

Or, if you want to scale just some columns, but don't drop the rest of the columns:

scale_cols = ['a','b']
resid_cols = df.drop(columns = scale_cols).columns
df = pd.concat([pd.DataFrame(scaler.fit_transform(df[scale_cols]),columns =scale_cols),df[resid_cols]],axis=1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.