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.
Feel 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 as np
DataFrame backfill() and bfill()
The DataFrame backfill()
and bfill()
methods backward fill missing data (such as np.nan
, None
, NaN
, and NaT
values) from the DataFrame/Series.
The syntax for these methods is as follows:
DataFrame.backfill(axis=None, inplace=False, limit=None, downcast=None) DataFrame.bfill(axis=None, inplace=False, limit=None, downcast=None)
axis | If zero (0) or index is selected, apply to each column. Default 0. If one (1) apply to each row. |
inplace | If set to True , the changes apply to the original DataFrame/Series. If False , the changes apply to a new DataFrame/Series. By default, False . |
limit | The maximum number of elements to backward fill. |
downcast | The only available selection is infer . This parameter attempts to convert floats (float64) to integers (int64). |
Throughout this article, we use the same DataFrame example. This DataFrame contains three (3) rows with missing data. Each example attempts to handle the missing data.
In this example, the DataFrame contains some missing data. This code will attempt to (replace) these values using the bfill()
method.
Code β Example 1
df = pd.DataFrame({'Data-1': [None, 11, 12], 'Data-2': [13, 14, None], 'Data-3': [None, 15, 16]}) print(df) result = df.bfill(axis='rows') print(result)
- Line [1] creates a dictionary of lists and saves it to
df
. - Line [2] outputs the DataFrame to the terminal. The missing values convert to
NaN
. - Line [3] backfills the
NaN
values across the rows. This output saves to theresult
variable. - Line [4] outputs the result to the terminal.
Output
df
Data-1 | Data-2 | Data-3 | |
0 | NaN | 13.0 | NaN |
1 | 11.0 | 14.0 | 15.0 |
2 | 12.0 | NaN | 16.0 |
result
Data-1 | Data-2 | Data-3 | |
0 | 11.0 | 13.0 | 15.0 |
1 | 11.0 | 14.0 | 15.0 |
2 | 12.0 | NaN | 16.0 |
π‘ Note: backfill
/bfill
tries to fill in the NaN
values with data from the same position in the next row. If there is no next row or the next row contains NaN
, the value does not change.
Code β Example 2
df = pd.DataFrame({'Data-1': [None, 11, 12], 'Data-2': [13, 14, 'NaN'], 'Data-3': [None, 15, 16]}) print(df) result = df.bfill(axis='rows') print(result)
- Line [1] creates a dictionary of lists and saves it to
df
. - Line [2] outputs the DataFrame to the terminal. The missing values convert to
NaN
. - Line [3] backfills the
NaN
values across the rows. This output saves to theresult
variable. - Line [4] outputs the result to the terminal.
Output
df
Data-1 | Data-2 | Data-3 | |
0 | NaN | 13.0 | NaN |
1 | 11.0 | 14.0 | 15.0 |
2 | 12.0 | NaN | 16.0 |
result
Data-1 | Data-2 | Data-3 | |
0 | 11.0 | 13.0 | 15.0 |
1 | 11.0 | 14.0 | 15.0 |
2 | 12.0 | NaN | 16.0 |
π‘Note: The output is identical to that in Example 1.
Code β Example 3
df = pd.DataFrame({'Data-1': [None, 11, 12], 'Data-2': [13, 14, 'NaN'], 'Data-3': [None, 15, 16]}) print(df) result = df.bfill(axis='rows', downcast='infer') print(result)
- Line [1] creates a dictionary of lists and saves it to
df
. - Line [2] outputs the DataFrame to the terminal. All missing values convert to
NaN
. - Line [3] backfills the
NaN
values across the rows. Theinfer
parameter attempts to change thedtype
across the DataFrame/Series. This output saves to theresult
variable. - Line [4] outputs the result to the terminal.
Output
df
Data-1 | Data-2 | Data-3 | |
0 | NaN | 13.0 | NaN |
1 | 11.0 | 14.0 | 15.0 |
2 | 12.0 | NaN | 16.0 |
result
Data-1 | Data-2 | Data-3 | |
0 | 11 | 13 | 15 |
1 | 11 | 14 | 15 |
2 | 12 | NaN | 16 |
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.