SettingWithCopyWarning
在尝试更改 DataFrame 中的某些值时,我遇到了常见的情况。我找到了一种方法来解决这个问题而不必禁用警告,但我觉得我做错了,而且它是不必要的浪费和计算效率低下。
label_encoded_feature_data_to_be_standardised_X_train = X_train_label_encoded[['price', 'vintage']]
label_encoded_feature_data_to_be_standardised_X_test = X_test_label_encoded[['price', 'vintage']]
label_encoded_standard_scaler = StandardScaler()
label_encoded_standard_scaler.fit(label_encoded_feature_data_to_be_standardised_X_train)
X_train_label_encoded_standardised = label_encoded_standard_scaler.transform(label_encoded_feature_data_to_be_standardised_X_train)
X_test_label_encoded_standardised = label_encoded_standard_scaler.transform(label_encoded_feature_data_to_be_standardised_X_test)
这就是它的设置方式,如果我这样做,我会收到警告:
X_train_label_encoded.loc[:,'price'] = X_train_label_encoded_standardised[:,0]
如果我这样做:
X_train_label_encoded_standardised_df = pd.DataFrame(data=X_train_label_encoded_standardised, columns=['price', 'vintage'])
我通过这样做解决了它:
X_train_label_encoded = X_train_label_encoded.drop('price', axis=1)
X_train_label_encoded['price'] = X_train_label_encoded_standardised_df.loc[:,'price']
这也有效:
X_train_label_encoded.replace(to_replace=X_train_label_encoded['price'], value=X_train_label_encoded_standardised_df['price'])
但即便如此,额外的 DataFrame 创建也会让人觉得过于笨拙。
为什么我不能以某种方式分配列?或者使用一些替换方法的安排?该文档似乎没有解决方案,还是我只是读错了?缺少一些明显但未详细说明的解决方案?
有没有更好的方法来做到这一点?