Pandas:如何根据条件替换数据框中的值

时间:2021-05-14 05:10:22

标签: python pandas dataframe

我正在尝试用我的第一个数据帧('test')的值替换我的第二个数据帧('area')的值。

我的输入图像:

enter image description here

问题是我只想替换不是 NaN 的值,因此,例如,area.iloc[0,1] 将是“6643.68”而不是“3321.84”,而是 area.iloc[-2, -1] 将是“19.66”而不是“NaN”。我原以为我可以做这样的事情:

area.loc[test.notnull()] = test

area.replace(area.loc[test.notnull()], test.notnull())

但这给了我错误“无法用多维键索引”。有任何想法吗?这应该很简单。

1 个答案:

答案 0 :(得分:0)

使用fillna,例如:

area.fillna(test)
相关问题