以正确的方式更新 Pandas Dataframe 中的列值

Updating column values in a Pandas Dataframe the correct way

我正在 kaggle 参加共同的初学者比赛,并意识到将年龄添加到分类器中会有所帮助。问题是,它的 Age 列有 NaN 值,我不想在整个 df 上填充所有 NaN,只是 Age 列。我应用下面的解决方案(通过获取中位数),然后定位行并像这样更新 _train['Age'] = X_train['Age'].fillna(X_train_median) 例如。

我知道这不是一个好习惯,但我收到以下错误

A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

是否有可能以更好的方式为 df 中符合特定条件的所有值更新特定列?下面的示例代码。

# IMPORT DATA 
train_data = pd.read_csv("data/train.csv")
test_data = pd.read_csv("data/test.csv")

# ASSIGN TO VAR
X_test = test_data
X = train_data
y = train_data["Survived"]

# SPLIT 
X_train, X_val, Y_train, Y_val = train_test_split(X, y, random_state=1)

# SELECTED FEATURES 
features = ["Pclass", "Sex", "SibSp", "Parch", "Embarked", "Age"]


# REMOVE NA's BY POPULATING WITH MEDIAN VAL
X_train_median = X_train['Age'].median()
X_val_median = X_val['Age'].median()
X_test_median = X_test['Age'].median()

X_train['Age'] = X_train['Age'].fillna(X_train_median)
X_val['Age'] = X_val['Age'].fillna(X_val_median)
X_test['Age'] = X_test['Age'].fillna(X_test_median)


# ONE HOT FOR CATAGORICAL VALS
X_train = pd.get_dummies(X_train[features])
X_val = pd.get_dummies(X_val[features])
X_test = pd.get_dummies(X_test[features])

我相信这应该有效:

X_train['Age'] = X_train.loc[:, 'Age'].fillna(X_train_median)
X_val['Age'] = X_val.loc[:, 'Age'].fillna(X_val_median)
X_test['Age'] = X_test.loc[:, 'Age'].fillna(X_test_median)

文档:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.loc.html

你可以试试,X 是一些 DataFrame:

X = X.assign(Age = X['Age'].fillna(value=X_median))