Sometimes data has missing values. Forward fill and backward fill help fill these gaps using nearby known values.
Forward fill and backward fill in 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.
df.fillna(method='ffill')df.fillna(method='bfill')df.fillna(method='ffill', limit=1)
df.fillna(method='bfill', axis=1)
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).
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)
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.
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.