两个dataframe组合成新的list后, 为什么操作list对原dataframe会产生影响?
kaggle里titantic的代码:
#导入数据
train_df = pd.read_csv(‘./data/train.csv’)
test_df = pd.read_csv(‘./data/test.csv’)
combine = [train_df, test_df]
#提取新特征
for dataset in combine:
dataset[‘Title’] = dataset.Name.str.extract(‘ ([A-Za-z]+).‘, expand=False)
问题描述:
首先使用train_df和test_df组成新的列表combine后,接着for循环对combine的dataset进行操作,为什么最后结果在train_df和test_df都增加了“Title”?请问中间的逻辑关系是什么?
多谢!
在这里,
combine
只是一个两个 DataFrame 地址 的列表, 经由combine
对两个 DataFrame 的变更操作, 都会变更 DataFrame 的内容.