Signup/Sign In

Python Pandas Series.bfill() Method

If we want to replace missing values or null values with others, we can choose the bfill method. In this tutorial, we will learn the python pandas Series.bfill() method which fills the null or missing values backward. It returns Series with the missing values filled or None if the inplace is True.

The below shows the syntax of the Series.bfill() method.

Syntax

Series.bfill(axis=None, inplace=False, limit=None, downcast=None)

Example: Fill missing values using the Series.bfill() method

Let's fill in the missing values present in the Series using the Series.bfill() method. This method backward fills the null or missing values present in the Series. See the below example.

#importing pandas as pd
import pandas as pd
#creating Series
series=pd.Series([None,5,None,10])
print("-----------The Series is---------")
print(series)
print("---------------------------------")
print(series.bfill())


-----------The Series is---------
0 NaN
1 5.0
2 NaN
3 10.0
dtype: float64
---------------------------------
0 5.0
1 5.0
2 10.0
3 10.0
dtype: float64

Example: Set limit in the Series.bfill() method

Here, in this example, we set the limit parameter to some integer. Using this parameter we can define a limit to the Series.bfill() method to fill the number of consecutive missing values. See the below example.

In the below example, the Series consists of three consecutive missing values and the Series.bfill() method fills only two missing values as the limit is set to 2.

#importing pandas as pd
import pandas as pd
#creating Series
series=pd.Series([None,None,None,5])
print("-----------The Series is---------")
print(series)
print("---------------------------------")
print(series.bfill(limit=2))


-----------The Series is---------
0 NaN
1 NaN
2 NaN
3 5.0
dtype: float64
---------------------------------
0 NaN
1 5.0
2 5.0
3 5.0
dtype: float64

Example: Set inplace=True in the Series.bfill() method

Here, in this example, we have set inplace=True in the Series.bfill() method. As this parameter is True, the Series.bfill() method fills the missing values without creating the new object and it returns None. See the below example.

#importing pandas as pd
import pandas as pd
#creating Series
series=pd.Series([None,5,None,5])
print("-----------The Series is---------")
print(series)
print("---------------------------------")
print(series.bfill(inplace=True))


-----------The Series is---------
0 NaN
1 5.0
2 NaN
3 5.0
dtype: float64
---------------------------------
None
0 5.0
1 5.0
2 5.0
3 5.0
dtype: float64

Conclusion

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



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.