Pandas DataFrame.update(~) 方法使用另一个 DataFrame 中的非 NaN 值替换源 DataFrame 中的值。
警告
更新是就地完成的,这意味着源DataFrame将被直接修改。
参数
1.other | Series 或 DataFrame
Series 或 DataFrame 保存用于更新源 DataFrame 的值。
- 
如果提供了 Series,则其名称属性必须与您要更新的列的名称匹配。
- 
如果提供了 DataFrame,则列名称必须匹配。
2. overwrite | boolean | optional
- 
如果是 True,则源 DataFrame 中的所有值都将使用other进行更新。
- 
如果 False,则仅使用other更新源 DataFrame 中的NaN值。
默认情况下,overwrite=True 。
3. filter_func | function | optional
您想要更新的值。该函数将一列作为一维 Numpy 数组,并返回一个布尔值的一维数组,指示是否应更新值。
4. errors | string | optional
是否引发错误:
| 值 | 说明 | 
|---|---|
| 
 | 如果非  | 
| 
 | 不会引发任何错误。 | 
默认情况下,errors="ignore" 。
返回值
由于更新是就地执行的,因此不会返回任何内容。这意味着源DataFrame将被直接修改。
例子
基本用法
考虑以下数据帧:
df = pd.DataFrame({"A":[1,2], "B":[3,4]})
df_other = pd.DataFrame({"B":[5,6], "C":[7,8]})
  [df]     [df_other]
   A  B        B  C
0  1  3     0  5  7
1  2  4     1  6  8请注意,两个 DataFrames 都有一个带有标签 B 的列。执行更新会给出:
df.update(df_other)
df
   A  B
0  1  5
1  2  6原始 DataFrame 列 B 中的值已被其他 DataFrame 列 B 中的值替换。
其他 DataFrame 包含缺失值的情况
考虑以下数据帧:
df = pd.DataFrame({"A":[1,2], "B":[3,4]})
df_other = pd.DataFrame({"B":[5,np.NaN], "C":[7,8]})
  [df]     [df_other]
   A  B        B    C 
0  1  3     0  5.0  7
1  2  4     1  NaN  8请注意 other DataFrame 如何具有 NaN 。
执行更新会给出:
df.update(df_other)
df
   A  B
0  1  5.0
1  2  4.0这里的要点是,如果新值是缺失值,则不会对该值执行更新。
指定覆盖参数
考虑以下数据帧:
df = pd.DataFrame({"A":[1,2], "B":[3,np.NaN]})
df_other = pd.DataFrame({"B":[5,6], "C":[7,8]})
   [df]      [df_other]
   A  B          B  C
0  1  3       0  5  7
1  2  NaN     1  6  8使用默认参数 overwrite=True 执行更新会给出:
df.update(df_other)
df
   A  B
0  1  5.0
1  2  6.0请注意源 DataFrame 的列 B 中的所有值是如何更新的。
现在,让我们将其与 overwrite=False 进行比较:
df.update(df_other, overwrite=False)
df
   A  B
0  1  3.0
1  2  6.0此处,值 3 保持不变,而 NaN 被替换为 6 的相应值。这是因为 overwrite=False 确保只有 NaN 得到更新,而非 NaN 值保持不变。
指定filter_func参数
考虑以下数据帧:
df = pd.DataFrame({"A":[1,2], "B":[3,4]})
df_other = pd.DataFrame({"B":[5,6], "C":[7,8]})
   [df]      [df_other]
   A  B          B  C
0  1  3       0  5  7
1  2  4       1  6  8假设我们只想更新大于 3 的值。我们可以通过指定一个自定义函数来做到这一点,如下所示:
def foo(vals):
   return vals > 3
df.update(df_other, filter_func=foo)
df
   A  B
0  1  3
1  2  6请注意值3 是如何保持不变的。
指定错误参数
考虑以下数据帧:
df = pd.DataFrame({"A":[1,2], "B":[3,4]})
df_other = pd.DataFrame({"B":[5,6]})
   [df]    [df_other]
   A  B          B
0  1  3       0  5
1  2  4       1  6使用默认参数 errors="ignore" 执行更新会给出:
df.update(df_other)   # errors="ignore"
df
   A  B
0  1  5
1  2  6即使使用非 NaN 值更新非 NaN 值,更新也不会出现任何错误。
使用 errors="raise" 执行更新会得到:
df.update(df_other, errors="raise")
df
ValueError: Data overlaps.我们最终会遇到错误,因为我们试图用非 NaN 值更新非 NaN 值。请注意,如果 df_other 中的 B 列仅将 NaN 作为值,则不会引发错误。
相关用法
- Python Pandas DataFrame unstack方法用法及代码示例
- Python PySpark DataFrame unionByName方法用法及代码示例
- Python PySpark DataFrame union方法用法及代码示例
- Python Pandas DataFrame empty属性用法及代码示例
- Python Pandas DataFrame pop方法用法及代码示例
- Python Pandas DataFrame nsmallest方法用法及代码示例
- Python Pandas DataFrame sample方法用法及代码示例
- Python Pandas DataFrame items方法用法及代码示例
- Python Pandas DataFrame max方法用法及代码示例
- Python Pandas DataFrame swaplevel方法用法及代码示例
- Python Pandas DataFrame agg方法用法及代码示例
- Python Pandas DataFrame copy方法用法及代码示例
- Python Pandas DataFrame pow方法用法及代码示例
- Python Pandas DataFrame insert方法用法及代码示例
- Python Pandas DataFrame lt方法用法及代码示例
- Python Pandas DataFrame all方法用法及代码示例
- Python Pandas DataFrame mean方法用法及代码示例
- Python PySpark DataFrame filter方法用法及代码示例
- Python Pandas DataFrame tz_convert方法用法及代码示例
- Python Pandas DataFrame isin方法用法及代码示例
- Python PySpark DataFrame collect方法用法及代码示例
- Python PySpark DataFrame intersect方法用法及代码示例
- Python PySpark DataFrame dtypes属性用法及代码示例
- Python Pandas DataFrame rank方法用法及代码示例
- Python Pandas DataFrame tail方法用法及代码示例
注:本文由纯净天空筛选整理自Isshin Inada大神的英文原创作品 Pandas DataFrame | update method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
