这是我到目前为止所拥有的:
data.shape: # data == my dataframe
(768, 9)
data2 = pd.DataFrame(data) # copy of data
array = data.values # convert data to arrays
X = array[:,0:8]
Y = array[:,8]
# perform a transformation on X
Xrescaled = scaler.transform(X)
如何使用数组data2
中的对应列替换数据框的每一列Xrescaled
?感谢。
答案 0 :(得分:2)
你可以这样做:data2.iloc[:,:8] = Xrescaled
,这是一个演示:
import numpy as np
data = pd.DataFrame({'x': [1,2], 'y': [3,4], 'z': [5,6]})
data
# x y z
#0 1 3 5
#1 2 4 6
import pandas as pd
data2 = pd.DataFrame(data)
data2
# x y z
#0 1 3 5
#1 2 4 6
X = data.values[:,:2]
Xrescaled = X * 2
Xrescaled
# array([[2, 6],
# [4, 8]])
data2.iloc[:,:2] = Xrescaled
data2
# x y z
#0 2 6 5
#1 4 8 6