- API reference
- DataFrame
- pandas.DataF...
pandas.DataFrame.update#
- DataFrame.update(other,join='left',overwrite=True,filter_func=None,errors='ignore')[source]#
Modify in place using non-NA values from another DataFrame.
Aligns on indices. There is no return value.
- Parameters:
- otherDataFrame, or object coercible into a DataFrame
Should have at least one matching index/column labelwith the original DataFrame. If a Series is passed,its name attribute must be set, and that will beused as the column name to align with the original DataFrame.
- join{‘left’}, default ‘left’
Only left join is implemented, keeping the index and columns of theoriginal object.
- overwritebool, default True
How to handle non-NA values for overlapping keys:
True: overwrite original DataFrame’s valueswith values fromother.
False: only update values that are NA inthe original DataFrame.
- filter_funccallable(1d-array) -> bool 1d-array, optional
Can choose to replace values other than NA. Return True for valuesthat should be updated.
- errors{‘raise’, ‘ignore’}, default ‘ignore’
If ‘raise’, will raise a ValueError if the DataFrame andotherboth contain non-NA data in the same place.
- Returns:
- None
This method directly changes calling object.
- Raises:
- ValueError
Whenerrors=’raise’ and there’s overlapping non-NA data.
Whenerrors is not either‘ignore’ or‘raise’
- NotImplementedError
Ifjoin != ‘left’
See also
dict.update
Similar method for dictionaries.
DataFrame.merge
For column(s)-on-column(s) operations.
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 B0 1 41 2 52 3 6
The DataFrame’s length does not increase as a result of the update,only values at matching index/column labels are updated.
>>>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 B0 a d1 b e2 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 B0 a d1 b y2 c f
For Series, its name attribute must be set.
>>>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 B0 a d1 b e2 c f
Ifother contains NaNs the corresponding values are not updatedin the original 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 B0 1 4.01 2 500.02 3 6.0