英文:
Pandas replace is executing over two separate dataframes
问题
我正在尝试创建一个临时的数据框,其中NaN值被替换为零,而不影响原始数据框。但是,我注意到当我用0替换df_2中的NaN值时,df_1中对应的列也发生了变化。在创建df_2时,我是否做错了什么?
代码
d = {'A':[np.nan, np.nan],'B':[1,2]}
df_1 = pd.DataFrame(data=d)
df_2 = df_1.copy() # 修正此处,使用copy方法创建df_2
print(df_1)
print(df_2)
df_2['A'] = df_2['A'].replace(np.nan,0)
print(df_1)
print(df_2)
输出
A B
0 NaN 1
1 NaN 2
A B
0 NaN 1
1 NaN 2
A B
0 0.0 1
1 0.0 2
A B
0 0.0 1
1 0.0 2
英文:
I'm trying to make a temporary dataframe where NaN values are replaced by zeros without affecting the original dataframe. However, I noticed that when I replace the NaN's of df_2 with 0s the corresponding column in df_1 is also changed. Have I done something wrong here when creating df_2?
Code
d = {'A':[np.nan, np.nan],'B':[1,2]}
df_1 = pd.DataFrame(data=d)
df_2 = df_1
print(df_1)
print(df_2)
df_2['A'] = df_2['A'].replace(np.nan,0)
print(df_1)
print(df_2)
Outputs
A B
0 NaN 1
1 NaN 2
A B
0 NaN 1
1 NaN 2
A B
0 0.0 1
1 0.0 2
A B
0 0.0 1
1 0.0 2
答案1
得分: 0
使用深拷贝
df_2 = df_1.copy()
更多信息请查看pandas.DataFrame.copy。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论