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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
