0
0
Pandasdata~5 mins

Forward fill and backward fill in Pandas

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 measurements and want to fill gaps with the last known value.
You want to fill missing data in a table by copying the next available value backward.
You need to prepare data for analysis or visualization without losing rows due to missing values.
You want to keep data continuity by filling missing entries with nearby known values.
You want a simple way to handle missing data before applying calculations.
Syntax
Pandas
DataFrame.fillna(method='ffill')  # forward fill
DataFrame.fillna(method='bfill')  # backward fill

ffill means forward fill: fill missing values with the last known value above.

bfill means backward fill: fill missing values with the next known value below.

Examples
Fill missing values by copying the last known value forward.
Pandas
df.fillna(method='ffill')
Fill missing values by copying the next known value backward.
Pandas
df.fillna(method='bfill')
Forward fill but only fill up to 1 consecutive missing value.
Pandas
df.fillna(method='ffill', limit=1)
Backward fill along columns instead of rows.
Pandas
df.fillna(method='bfill', axis=1)
Sample Program

This code creates a table with some missing numbers. It shows the original table, then fills missing spots by copying the last known value down (forward fill), and then by copying the next known value up (backward fill).

Pandas
import pandas as pd
import numpy as np

# Create a DataFrame with missing values
data = {'A': [1, np.nan, np.nan, 4, 5],
         'B': [np.nan, 2, np.nan, 4, np.nan],
         'C': [1, 2, 3, np.nan, 5]}

df = pd.DataFrame(data)

print('Original DataFrame:')
print(df)

# Forward fill missing values
ffill_df = df.fillna(method='ffill')
print('\nDataFrame after forward fill:')
print(ffill_df)

# Backward fill missing values
bfill_df = df.fillna(method='bfill')
print('\nDataFrame after backward fill:')
print(bfill_df)
OutputSuccess
Important Notes

If missing values are at the start (for forward fill) or end (for backward fill), they may remain missing because there is no value to copy.

You can use the limit parameter to control how many consecutive missing values get filled.

Forward and backward fill work well for ordered data like time series.

Summary

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

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

These methods help keep data complete for analysis without guessing new values.