python function to scale selected features in a dataframe pandas 1

python function to scale selected features in a dataframe pandas

# make a copy of dataframe
scaled_features = df.copy()

col_names = ['co_1', 'col_2', 'col_3', 'col_4']
features = scaled_features[col_names]

# Use scaler of choice; here Standard scaler is used
scaler = StandardScaler().fit(features.values)
features = scaler.transform(features.values)

scaled_features[col_names] = features

Here is what the above code is Doing:
1. Make a copy of the dataframe
2. Select the columns to be scaled
3. Fit the scaler to the selected columns
4. Transform the selected columns with the scaler
5. Replace the values in the dataframe with the scaled values

Similar Posts