Preparation
Before any data manipulation can occur, two (2) new libraries will require installation.
- The Pandas library enables access to/from a DataFrame.
- The NumPy library supports multi-dimensional arrays and matrices in addition to a collection of mathematical functions.
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
DataFrame update()
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)
- Line [1] reads in the first three (3) records of the comma-delimited CSV file and saves to
df1
. - Line [2] sets the amount of the increase applied to the Population column.
- Line [3] applies the increase (amt) to each Population cell and converts this column to a list. The output saves to
df2
. - Line [4] updates
df1
with the new Population values residing indf2
. - Line [5] outputs the updated DataFrame to the terminal.
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.