Examples
import numpy as np
import pandas as pd
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
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({'M': ['m', 'n', 'o'],
'N': ['x', 'y', 'z']})
new_df = pd.DataFrame({'N': ['p', 'q', 'r', 's', 't', 'v']})
df.update(new_df)
df
For Series, it’s name attribute must be set.
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
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
If other contains NaNs the corresponding values are not updated in the original dataframe.
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