Sometimes data has missing values. Forward fill and backward fill help fill these gaps using nearby known values.
Forward fill and backward fill in 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).
df.fillna(method='ffill')df.fillna(method='bfill')df.fillna(method='ffill', limit=1)
This code creates a small table with missing temperatures. It shows how forward fill and backward fill replace missing values using nearby known temperatures.
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)
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.
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.