Pandas的replace正在执行在两个不同的数据框上。

huangapple go评论162阅读模式
英文:

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

英文:

Use deep copy

df_2 = df_1.copy()

See more in pandas.DataFrame.copy

huangapple
  • 本文由 发表于 2023年7月17日 11:33:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701339.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定