Pandas update()

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 with
  • join (optional): specifies which of the two objects to update
  • overwrite (optional): specifies whether to overwrite NULL values or not
  • filter_func (optional): specifies a function to execute for each replaced element
  • errors (optional): if 'raise', a ValueError will be raised if both DataFrames have a NULL 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.

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges