Pandas DataFrame backfill() and bfill() Method


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)
axisIf zero (0) or index is selected, apply to each column. Default 0.
If one (1) apply to each row.
inplaceIf set to True, the changes apply to the original DataFrame/Series. If False, the changes apply to a new DataFrame/Series. By default, False.
limitThe maximum number of elements to backward fill.
downcastThe 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 the result variable.
  • Line [4] outputs the result to the terminal.

Output

df
 Data-1 Data-2 Data-3
0NaN13.0NaN
111.014.015.0
212.0NaN16.0
result
 Data-1 Data-2 Data-3
011.013.015.0
111.014.015.0
212.0NaN16.0

πŸ’‘ Notebackfill/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 the result variable.
  • Line [4] outputs the result to the terminal.

Output

df
 Data-1 Data-2 Data-3
0NaN13.0NaN
111.014.015.0
212.0NaN16.0
result
 Data-1 Data-2 Data-3
011.013.015.0
111.014.015.0
212.0NaN16.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. The infer parameter attempts to change the dtype across the DataFrame/Series. This output saves to the result variable.
  • Line [4] outputs the result to the terminal.

Output

df
 Data-1 Data-2 Data-3
0NaN13.0NaN
111.014.015.0
212.0NaN16.0
result
 Data-1 Data-2 Data-3
0111315
1111415
212NaN16

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.