Big*_*ata 2 python standardized pandas sklearn-pandas
下面的Python代码仅向我返回一个数组,但我希望将缩放后的数据替换为原始数据。
from sklearn.preprocessing import StandardScaler
df = StandardScaler().fit_transform(df[['cost', 'sales']])
df
Run Code Online (Sandbox Code Playgroud)
输出
array([[ 1.99987622, -0.55900276],
[-0.49786658, -0.45658181],
[-0.5146864 , -0.505097 ],
[-0.48104676, -0.47814412],
[-0.50627649, 1.9988257 ]])
Run Code Online (Sandbox Code Playgroud)
原始数据
id cost sales item
1 300 50 pen
2 3 88 bottle
3 1 70 drink
4 5 80 cup
5 2 999 ink
Run Code Online (Sandbox Code Playgroud)
只需将其分配回来
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5458 次 |
| 最近记录: |