Search code examples
pythonpandassklearn-pandasstandardized

Standardize some columns in Python Pandas dataframe?


Python code below only return me an array, but I want the scaled data to replace the original data.

from sklearn.preprocessing import StandardScaler
df = StandardScaler().fit_transform(df[['cost', 'sales']])
df

output

array([[ 1.99987622, -0.55900276],
       [-0.49786658, -0.45658181],
       [-0.5146864 , -0.505097  ],
       [-0.48104676, -0.47814412],
       [-0.50627649,  1.9988257 ]])

original data

id  cost    sales   item
1   300       50    pen
2   3         88    bottle
3   1         70    drink
4   5         80    cup
5   2        999    ink

Solution

  • Simply assign it back

    df[['cost', 'sales']] = StandardScaler().fit_transform(df[['cost', 'sales']])
    df
    Out[45]: 
       id      cost     sales    item
    0   1  1.999876 -0.559003     pen
    1   2 -0.497867 -0.456582  bottle
    2   3 -0.514686 -0.505097   drink
    3   4 -0.481047 -0.478144     cup
    4   5 -0.506276  1.998826     ink