How to Scale Data Using Standard Scaler But Keep Column Names

Python

In this code snippet we demonstrate how to scale data using Sklearn StandardScaler and then convert the transformed data back into a DataFrame with the column names of the original DataFrame.

 1|  from sklearn.preprocessing import StandardScaler
 2|  
 3|  scaler = StandardScaler()
 4|  scaler.fit(X_train)
 5|  X_train = pd.DataFrame(scaler.transform(X_train), columns = X_train.columns)
 6|  X_test = pd.DataFrame(scaler.transform(X_test), columns = X_test.columns)
Did you find this snippet useful?

Sign up for free to to add this to your code library