两个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”?请问中间的逻辑关系是什么?
多谢!

Jason990420
最佳答案
combine = [train_df, test_df]

在这里, combine 只是一个两个 DataFrame 地址 的列表, 经由 combine 对两个 DataFrame 的变更操作, 都会变更 DataFrame 的内容.

1年前 评论
讨论数量: 1
Jason990420
combine = [train_df, test_df]

在这里, combine 只是一个两个 DataFrame 地址 的列表, 经由 combine 对两个 DataFrame 的变更操作, 都会变更 DataFrame 的内容.

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!