标准化 Python Pandas 数据框中的某些列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49641707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:25:02  来源:igfitidea点击:

Standardize some columns in Python Pandas dataframe?

pythonpandassklearn-pandasstandardized

提问by BigData

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

下面的 Python 代码只返回一个数组,但我希望缩放数据替换原始数据。

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

回答by YOBEN_S

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