Python 3.x 在将standardscaler应用于其中一个数据帧之后,如何合并回两个数据帧?
我在合并我的两个dfs时遇到问题。我有一个5列的数据帧,4列是二进制的,一列是连续的。因此,我只想将standardscaler应用于一个:Python 3.x 在将standardscaler应用于其中一个数据帧之后,如何合并回两个数据帧?,python-3.x,pandas,dataframe,Python 3.x,Pandas,Dataframe,我在合并我的两个dfs时遇到问题。我有一个5列的数据帧,4列是二进制的,一列是连续的。因此,我只想将standardscaler应用于一个: scaler = StandardScaler() numeric_df = pd.DataFrame(scaler.fit_transform(X_train['temperature']),columns=['temperature']) X_train_new = X_train.drop(columns=['temperature']) resu
scaler = StandardScaler()
numeric_df = pd.DataFrame(scaler.fit_transform(X_train['temperature']),columns=['temperature'])
X_train_new = X_train.drop(columns=['temperature'])
result =pd.concat([numeric_df,X_train_new],axis=1)
但是,结果没有正确追加,它的行数比数值_df和x-train-new多?当这些数据帧的长度相等时,这是怎么可能的
当使用标准定标器后,数值_df有一个重置的索引时,我如何确保当我将它们也合并时,它们是基于原始索引的
pd.concat([X_train_new.reset_index(),numeric_df],axis=1)
当我做上述的时候,它是如何工作的?我不明白为什么我需要这样做,如果在重置两个数据帧的索引后,它对应于原始的,这是因为两个数据帧中的索引不同。尝试:
# this has range index
numeric_df = pd.DataFrame(scaler.fit_transform(X_train['temperature']),columns=['temperature'])
# so we make this with range index as well
X_train_new = X_train.drop(columns=['temperature']).reset_index(drop=True)
result =pd.concat([numeric_df,X_train_new],axis=1)
或者如果您希望保留X\u列车的索引
:
numeric_df = pd.DataFrame(scaler.fit_transform(X_train['temperature']),
columns=['temperature'],
index=X_train.index) # here
X_train_new = X_train.drop(columns=['temperature'])
result =pd.concat([numeric_df,X_train_new],axis=1)