Examples

In [1]:
import numpy as np
import pandas as pd
In [2]:
df = pd.DataFrame({'P': [2, 3, 4],
                   'Q': [500, 600, 700]})
new_df = pd.DataFrame({'Q': [5, 6, 7],
                       'R': [8, 9, 10]})
df.update(new_df)
df
Out[2]:
P Q
0 2 5
1 3 6
2 4 7

The DataFrame’s length does not increase as a result of the update, only values at matching
index/column labels are updated.

In [3]:
df = pd.DataFrame({'M': ['m', 'n', 'o'],
                   'N': ['x', 'y', 'z']})
new_df = pd.DataFrame({'N': ['p', 'q', 'r', 's', 't', 'v']})
df.update(new_df)
df
Out[3]:
M N
0 m p
1 n q
2 o r

For Series, it’s name attribute must be set.

In [4]:
df = pd.DataFrame({'M': ['m', 'n', 'o'],
                   'N': ['x', 'y', 'z']})
new_column = pd.Series(['p', 'q'], name='N', index=[0, 2])
df.update(new_column)
df
Out[4]:
M N
0 m p
1 n y
2 o q
In [5]:
df = pd.DataFrame({'M': ['m', 'n', 'o'],
                   'N': ['x', 'y', 'z']})
new_df = pd.DataFrame({'N': ['p', 'q']}, index=[1, 2])
df.update(new_df)
df
Out[5]:
M N
0 m x
1 n p
2 o q

If other contains NaNs the corresponding values are not updated in the original dataframe.

In [6]:
df = pd.DataFrame({'M': [2, 3, 4],
                   'N': [500, 600, 700]})
new_df = pd.DataFrame({'N': [5, np.nan, 7]})
df.update(new_df)
df
Out[6]:
M N
0 2 5.0
1 3 600.0
2 4 7.0