Pandas DataFrame.update() 函数中的 `overwrite` 参数是什么意思?

7

我阅读了这个文档,但我不理解overwrite选项对于update过程实际上是做什么的。我进行了几次测试,但无论我是否将overwrite设置为TrueFalse都没有任何区别。能否有人给出一个例子,在这个例子中overwrite会有所不同?

1个回答

4

overwrite 被设置为false时的区别在于,它只会填充在调用了update函数的 DataFrame 中缺失的值。

根据您提供的链接中的示例(使用默认值overwrite=True):

df = pd.DataFrame({'A': [1, 2,3], 'B': [400, None, 600]})
new_df = pd.DataFrame({'B': [4, 5, 6],  'C': [7, 8, 9]})
df.update(new_df)

产出:

   A    B
0  1  4.0
1  2  5.0
2  3  6.0

相反,df.update(new_df, overwrite=False) 产生以下结果:

   A      B
0  1  400.0
1  2    5.0
2  3  600.0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接