pandas.DataFrame.update#

DataFrame.update(other, join='left', overwrite=True, filter_func=None, errors='ignore')[源代码]#

使用另一个DataFrame中的非NA值原地修改。

按索引对齐。没有返回值。

Parameters:
otherDataFrame,或可转换为 DataFrame 的对象。

应该至少有一个与原始 DataFrame 匹配的索引/列标签。如果传递了 Series,则必须设置其 name 属性,该属性将用作与原始 DataFrame 对齐的列名。

<strong>join</strong>{‘left’},默认为 ‘left’。

仅实现了左连接,保留了原始对象的索引和列。

overwritebool, default True

如何处理重叠键的非 NA 值:

  • True:用 other 中的值覆盖原始 DataFrame 的值。

  • False:仅更新原始 DataFrame 中为 NA 的值。

filter_funccallable(1d-array) -> bool 1d-array,可选。

可以选择替换非 NA 的值。返回 True 表示应更新的值。

errors{‘raise’, ‘ignore’},默认为 ‘ignore’。

如果为 ‘raise’,当 DataFrame 和 other 在同一位置都包含非 NA 数据时,将引发 ValueError。

Returns:
None

此方法直接修改调用对象。

Raises:
ValueError
  • errors=’raise’ 且存在重叠的非 NA 数据时。

  • errors 不是 ‘ignore’‘raise’ 时。

NotImplementedError
  • 如果 join != ‘left’

参见

dict.update

字典的相似方法。

DataFrame.merge

包含来自调用者和 other 的列的 DataFrame。

Examples

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

更新后 DataFrame 的长度不会增加,只会更新匹配的索引/列标签处的值。

>>> df = pd.DataFrame({'A': ['a', 'b', 'c'],
...                    'B': ['x', 'y', 'z']})
>>> new_df = pd.DataFrame({'B': ['d', 'e', 'f', 'g', 'h', 'i']})
>>> df.update(new_df)
>>> df
   A  B
0  a  d
1  b  e
2  c  f
>>> df = pd.DataFrame({'A': ['a', 'b', 'c'],
...                    'B': ['x', 'y', 'z']})
>>> new_df = pd.DataFrame({'B': ['d', 'f']}, index=[0, 2])
>>> df.update(new_df)
>>> df
   A  B
0  a  d
1  b  y
2  c  f

对于 Series,必须设置其 name 属性。

>>> df = pd.DataFrame({'A': ['a', 'b', 'c'],
...                    'B': ['x', 'y', 'z']})
>>> new_column = pd.Series(['d', 'e', 'f'], name='B')
>>> df.update(new_column)
>>> df
   A  B
0  a  d
1  b  e
2  c  f

如果 other 包含 NaN,则原始 DataFrame 中的相应值不会被更新。

>>> df = pd.DataFrame({'A': [1, 2, 3],
...                    'B': [400., 500., 600.]})
>>> new_df = pd.DataFrame({'B': [4, np.nan, 6]})
>>> df.update(new_df)
>>> df
   A      B
0  1    4.0
1  2  500.0
2  3    6.0