0
0
Data Analysis Pythondata~5 mins

Forward fill and backward fill in Data Analysis Python

Choose your learning style9 modes available
Introduction

Sometimes data has missing values. Forward fill and backward fill help fill these gaps using nearby known values.

You have a time series with missing sensor readings and want to fill gaps using the last known value.
You want to fill missing daily sales data by carrying forward the previous day's sales.
You have survey data with missing answers and want to fill them using the next available answer.
You want to prepare data for analysis by replacing missing values with nearby known values.
You want to smooth data by filling missing spots without guessing new values.
Syntax
Data Analysis Python
DataFrame.fillna(method='ffill')  # forward fill
DataFrame.fillna(method='bfill')  # backward fill

ffill means fill missing values using the last known value above (forward fill).

bfill means fill missing values using the next known value below (backward fill).

Examples
Fill missing values by copying the last known value forward.
Data Analysis Python
df.fillna(method='ffill')
Fill missing values by copying the next known value backward.
Data Analysis Python
df.fillna(method='bfill')
Forward fill but only fill up to 1 consecutive missing value.
Data Analysis Python
df.fillna(method='ffill', limit=1)
Sample Program

This code creates a small table with missing temperatures. It shows how forward fill and backward fill replace missing values using nearby known temperatures.

Data Analysis Python
import pandas as pd

# Create example data with missing values
data = {'Day': [1, 2, 3, 4, 5],
        'Temperature': [20.0, None, None, 22.0, None]}
df = pd.DataFrame(data)

# Forward fill missing values
ffill_df = df.fillna(method='ffill')

# Backward fill missing values
bfill_df = df.fillna(method='bfill')

print('Original DataFrame:')
print(df)
print('\nAfter Forward Fill:')
print(ffill_df)
print('\nAfter Backward Fill:')
print(bfill_df)
OutputSuccess
Important Notes

Forward fill works well when missing data should carry the last known value forward in time.

Backward fill is useful when you want to fill missing data using the next known value.

Be careful: filling too many missing values blindly can hide real gaps or errors.

Summary

Forward fill copies the last known value forward to fill missing spots.

Backward fill copies the next known value backward to fill missing spots.

Both help prepare data for analysis by handling missing values simply.