Pandas DataFrame update() Method – Be on the Right Side of Change (original) (raw)


Preparation

Before any data manipulation can occur, two (2) new libraries will require installation.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install numpy

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.


FeFeel free to view the PyCharm installation guide for the required libraries.


Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd import numpy


The update() method performs modifications in-place using non-NA values from a second DataFrame/Series. This method aligns with the index(es).

The syntax for this method is as follows:

DataFrame.update(other, join='left', overwrite=True, filter_func=None, errors='ignore'

Parameter Description
other Must have (minimum) one matching column/index with the original DataFrame. If a Series, the name attribute is used as the align column with the original DataFrame.
join Left is the only option. This option keeps the index/columns of the original DataFrame/Series.
overwrite This parameter determines how to deal with non-NA values for over-lapping keys. – If True, over-write original with values from other. By default, True. – If False, only update values that are NA in the original.
filter_func This parameter takes a 1-dimensional array or 1-dimension Boolean array.
errors If ‘raise’ is selected, a ValueError occurs if both originating and other contain non-NA values in the same position.

For this example, the first three (3) records of the countries.csv file are read in. The population is increased and updated.

df1 = pd.read_csv('countries.csv').head(3) amt = 1.4 tmp = list(df1['Population'].apply(lambda x: x*amt)) df2 = pd.DataFrame({'Population': tmp}) df1.update(df2, overwrite=True) print(df1)


More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.