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

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

Pandas replace is executing over two separate dataframes

问题

我正在尝试创建一个临时的数据框,其中NaN值被替换为零,而不影响原始数据框。但是,我注意到当我用0替换df_2中的NaN值时,df_1中对应的列也发生了变化。在创建df_2时,我是否做错了什么?

代码

  1. d = {'A':[np.nan, np.nan],'B':[1,2]}
  2. df_1 = pd.DataFrame(data=d)
  3. df_2 = df_1.copy() # 修正此处,使用copy方法创建df_2
  4. print(df_1)
  5. print(df_2)
  6. df_2['A'] = df_2['A'].replace(np.nan,0)
  7. print(df_1)
  8. print(df_2)

输出

  1. A B
  2. 0 NaN 1
  3. 1 NaN 2
  4. A B
  5. 0 NaN 1
  6. 1 NaN 2
  7. A B
  8. 0 0.0 1
  9. 1 0.0 2
  10. A B
  11. 0 0.0 1
  12. 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

  1. d = {'A':[np.nan, np.nan],'B':[1,2]}
  2. df_1 = pd.DataFrame(data=d)
  3. df_2 = df_1
  4. print(df_1)
  5. print(df_2)
  6. df_2['A'] = df_2['A'].replace(np.nan,0)
  7. print(df_1)
  8. print(df_2)

Outputs

  1. A B
  2. 0 NaN 1
  3. 1 NaN 2
  4. A B
  5. 0 NaN 1
  6. 1 NaN 2
  7. A B
  8. 0 0.0 1
  9. 1 0.0 2
  10. A B
  11. 0 0.0 1
  12. 1 0.0 2

答案1

得分: 0

使用深拷贝

  1. df_2 = df_1.copy()

更多信息请查看pandas.DataFrame.copy

英文:

Use deep copy

  1. 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:

确定