Pandas DataFrame backfill() Method

In this tutorial, we will learn the python pandas DataFrame.backfill() method. This method fills the missing values in the dataframe in backward. This method is similar to the DataFrame.fillna() method with method='bfill'.

The below shows the syntax of DataFrame.backfill() method.

Syntax

DataFrame.backfill(axis=None, inplace=False, limit=None, downcast=None)

Parameters

axis: '0' or index. 1 and columns are not supported.

in place: It is boolean, True, or False. The default is False. If it is True, fills in the missing values but do not create a new object.

limit: It is int, default is None. If the method is specified, this is the maximum number of consecutive NaN values to backward fill.

Example: Create the DataFrame

Create a DataFrame with None values and print the output. In this tutorial, we will use this DataFrame.

import pandas as pd
df = pd.DataFrame({'A': [None, 3, None, None],'B': [2, 4, None, 3],'C': [None, None, None, 1],'D': [0, 1, 5, 4]}, columns=['A', 'B', 'C', 'D'])
print(df)

Once we run the program we will get the following output.


A B C D
0 NaN 2.0 NaN 0
1 3.0 4.0 NaN 1
2 NaN NaN NaN 5
3 NaN 3.0 1.0 4

Example 1: Fill in the missing values of DataFrame using DataFrame.backfill() Method

The below example shows how the DataFrame.backfill() method fills the missing values.

import pandas as pd
df = pd.DataFrame({'A': [None, 3, None, None],'B': [2, 4, None, 3],'C': [None, None, None, 1],'D': [0, 1, 5, 4]}, columns=['A', 'B', 'C', 'D'])
print(df.bfill())

Once we run the program we will get the following output.


A B C D
0 3.0 2.0 1.0 0
1 3.0 4.0 1.0 1
2 NaN 3.0 1.0 5
3 NaN 3.0 1.0 4

Example 2: Fill in the missing values of DataFrame using DataFrame.backfill() method with axis=1

This example is similar to the previous example the DataFrame.backfill() method fills the missing values with axis=1.

import pandas as pd
df = pd.DataFrame({'A': [None, 3, None, None],'B': [2, 4, None, 3],'C': [None, None, None, 1],'D': [0, 1, 5, 4]}, columns=['A', 'B', 'C', 'D'])
print(df.bfill(axis=1))

Once we run the program we will get the following output.


A B C D
0 2.0 2.0 0.0 0.0
1 3.0 4.0 1.0 1.0
2 5.0 5.0 5.0 5.0
3 3.0 3.0 1.0 4.0

Example 3: Fill in the missing values of DataFrame using DataFrame.backfill() method by setting limit value

We can set the limit value in DataFrame.backfill() method. This represents the maximum number of consecutive NaN values to backward fill.

import pandas as pd
df = pd.DataFrame({'A': [None, 3, None, None],'B': [2, 4, None, 3],'C': [None, None, None, 1],'D': [0, 1, 5, 4]}, columns=['A', 'B', 'C', 'D'])
print(df.bfill(limit=2))

Once we run the program we will get the following output.


A B C D
0 3.0 2.0 NaN 0
1 3.0 4.0 NaN 1
2 NaN 3.0 1.0 5
3 NaN 3.0 1.0 4

Example 4: Fill in the missing values of DataFrame using DataFrame.backfill() method with inplace=True

If inplace=True in the DataFrame.backfill() method, it will fill the missing values of the DataFrame but do not create a new object. If we want to check whether missing values are filled or not, we can check by printing the DataFrame.

import pandas as pd
df = pd.DataFrame({'A': [None, 3, None, None],'B': [2, 4, None, 3],'C': [None, None, None, 1],'D': [0, 1, 5, 4]}, columns=['A', 'B', 'C', 'D'])
print(df.bfill(inplace=True))
print(df)

Once we run the program we will get the following output.


None
A B C D
0 3.0 2.0 1.0 0
1 3.0 4.0 1.0 1
2 NaN 3.0 1.0 5
3 NaN 3.0 1.0 4

Conclusion

In this tutorial, we learned the Python pandas DataFrame.backfill() method. We learned syntax and parameters of the function and we applied this function on DataFrame that consisting of None values and understood DataFrame.backfill() method backward fill the NaN values that are present in the pandas dataframe.