The update()
method in Pandas is used to modify a dataframe with values from another dataframe, where only the matching index and column labels are updated.
Example
import pandas as pd
# create DataFrames
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [400, 500, 600]})
df2 = pd.DataFrame({'B': [4, 5, 6], 'C': [7, 8, 9]})
# update df1 with the matching column labels of df2
df1.update(df2)
print(df1)
'''
Output
A B
0 1 4
1 2 5
2 3 6
'''
update() Syntax
The syntax of the update()
method in Pandas is:
df.update(other, join='left', overwrite=True, filter_func=None, errors='ignore')
update() Arguments
The update()
method takes the following arguments:
other
: another dataframe to update the DataFrame withjoin
(optional): specifies which of the two objects to updateoverwrite
(optional): specifies whether to overwriteNULL
values or notfilter_func
(optional): specifies a function to execute for each replaced elementerrors
(optional): if'raise'
, aValueError
will be raised if both DataFrames have aNULL
value for the same element.
update() Return Value
The update()
method updates the DataFrame in place and returns None
.
Example 1: Update Without Overwriting Non-Missing Values
import pandas as pd
# create DataFrames
df1 = pd.DataFrame({'A': [1, None, 3], 'B': [400, 500, None]})
df2 = pd.DataFrame({'A': [None, 5, None], 'B': [4, None, 6]})
# update df1 with df2
df1.update(df2, overwrite=False)
print(df1)
Output
A B 0 1.0 400.0 1 5.0 500.0 2 3.0 6.0
In this example, we replaced only the null values in df1 while keeping the original not-null values using overwrite=False
.
Example 3: Specify Values to Update Using Filter Function
import pandas as pd
# create DataFrames
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [400, 500, 600]})
df2 = pd.DataFrame({'B': [4, 5, 6], 'C': [7, 8, 9]})
# define a filter function
def filter_func(x):
return x > 100
# update df1 with df2 using a filter function
df1.update(df2, filter_func=filter_func)
print(df1)
Output
A B 0 1 4 1 2 5 2 3 6
In this example, we used a filter function to specify which values to update.
Here, we updated the values greater than 100 only.