Python Pandas – Propagate non-null values backward

In pandas, backward fill propagates non-null values backward to fill missing data. Use the fillna() method with method='bfill' to replace NaN values with the next valid observation.

Syntax

DataFrame.fillna(method='bfill')

Creating Sample Data

Let's create a DataFrame with missing values to demonstrate backward fill ?

import pandas as pd
import numpy as np

# Create sample data with NaN values
data = {
    'Car': ['BMW', 'Lexus', 'Audi', 'Jaguar', 'Mustang'],
    'Reg_Price': [2500, 3500, 2500, 2000, 2500],
    'Units': [100.0, np.nan, 120.0, np.nan, 110.0]
}

dataFrame = pd.DataFrame(data)
print("DataFrame with NaN values...")
print(dataFrame)
DataFrame with NaN values...
       Car  Reg_Price  Units
0      BMW       2500  100.0
1    Lexus       3500    NaN
2     Audi       2500  120.0
3   Jaguar       2000    NaN
4  Mustang       2500  110.0

Using Backward Fill

Apply backward fill to propagate non-null values from subsequent rows ?

import pandas as pd
import numpy as np

# Create sample data
data = {
    'Car': ['BMW', 'Lexus', 'Audi', 'Jaguar', 'Mustang'],
    'Reg_Price': [2500, 3500, 2500, 2000, 2500],
    'Units': [100.0, np.nan, 120.0, np.nan, 110.0]
}

dataFrame = pd.DataFrame(data)
print("Original DataFrame:")
print(dataFrame)

# Propagate non-null values backward
result = dataFrame.fillna(method='bfill')
print("\nDataFrame after backward fill:")
print(result)
Original DataFrame:
       Car  Reg_Price  Units
0      BMW       2500  100.0
1    Lexus       3500    NaN
2     Audi       2500  120.0
3   Jaguar       2000    NaN
4  Mustang       2500  110.0

DataFrame after backward fill:
       Car  Reg_Price  Units
0      BMW       2500  100.0
1    Lexus       3500  120.0
2     Audi       2500  120.0
3   Jaguar       2000  110.0
4  Mustang       2500  110.0

How It Works

Backward fill looks for the next non-null value in the same column and fills the NaN with that value. In the example above:

  • Row 1 (Lexus): NaN is filled with 120.0 from row 2
  • Row 3 (Jaguar): NaN is filled with 110.0 from row 4

Alternative Syntax

You can also use the bfill() method directly ?

import pandas as pd
import numpy as np

data = {
    'Values': [1.0, np.nan, 3.0, np.nan, 5.0]
}

df = pd.DataFrame(data)
print("Original:")
print(df)

# Using bfill() method
result = df.bfill()
print("\nAfter bfill():")
print(result)
Original:
   Values
0     1.0
1     NaN
2     3.0
3     NaN
4     5.0

After bfill():
   Values
0     1.0
1     3.0
2     3.0
3     5.0
4     5.0

Conclusion

Use fillna(method='bfill') or bfill() to propagate non-null values backward in pandas DataFrames. This method fills missing values with the next valid observation in the same column.

Updated on: 2026-03-26T13:28:38+05:30

312 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements