How to Scale All Values For a Specified Column in Python


In this example we will show how to scale all values for specified columns in Python.

Source Code

import pandas as pd
from sklearn.preprocessing import StandardScaler

df = pd.read_csv("advertising.csv")

X = df[["TV", "radio", "newspaper"]]

scaler = StandardScaler()
scaled_X = scaler.fit_transform(X)

print(scaled_X)

Output:

[[ 9.69852266e-01  9.81522472e-01  1.77894547e+00]
 [-1.19737623e+00  1.08280781e+00  6.69578760e-01]
 [-1.51615499e+00  1.52846331e+00  1.78354865e+00]
 [ 5.20496822e-02  1.21785493e+00  1.28640506e+00]
 [ 3.94182198e-01 -8.41613655e-01  1.28180188e+00]
 [-1.61540845e+00  1.73103399e+00  2.04592999e+00]
 [-1.04557682e+00  6.43904671e-01 -3.24708413e-01]
 [-3.13436589e-01 -2.47406325e-01 -8.72486994e-01]
 [-1.61657614e+00 -1.42906863e+00 -1.36042422e+00]
 [ 6.16042873e-01 -1.39530685e+00 -4.30581584e-01]
 [-9.45155670e-01 -1.17923146e+00 -2.92486143e-01]]
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments