0
0
Data Analysis Pythondata~10 mins

Forward fill and backward fill in Data Analysis Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Forward fill and backward fill
Start with DataFrame with missing values
Choose fill method: forward fill or backward fill
Forward fill
Fill missing with
previous valid
Return filled DataFrame
Start with data having missing values, choose forward or backward fill to replace missing values with previous or next valid data, then get the filled data.
Execution Sample
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'A': [1, None, None, 4, None, 6]})

ffill_df = df.ffill()
bfill_df = df.bfill()
This code creates a DataFrame with missing values and fills them forward and backward.
Execution Table
StepIndexOriginal ValueForward Fill ValueBackward Fill ValueExplanation
10111No missing, value stays the same
21NaN14Forward fill uses previous 1, backward fill uses next 4
32NaN14Same as above, fills missing
43444No missing, value stays the same
54NaN46Forward fill uses previous 4, backward fill uses next 6
65666No missing, value stays the same
7----End of data, all missing filled
💡 All missing values replaced by forward or backward fill, no NaNs remain.
Variable Tracker
VariableStartAfter ffillAfter bfill
df['A'][1, NaN, NaN, 4, NaN, 6][1, 1, 1, 4, 4, 6][1, 4, 4, 4, 6, 6]
Key Moments - 3 Insights
Why does forward fill replace missing values with the previous valid value?
Forward fill looks up the column and fills each missing spot with the last known valid value above it, as shown in execution_table rows 2 and 3.
What happens if the first value is missing when using forward fill?
Forward fill cannot fill missing values at the start because there is no previous valid value; those remain NaN unless backward fill or other methods are used.
How does backward fill differ from forward fill in filling missing values?
Backward fill fills missing values using the next valid value below, as shown in execution_table rows 2 and 3 where missing values are replaced by 4 instead of 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the forward fill value at index 4?
ANaN
B4
C6
D1
💡 Hint
Check row 5 in the execution_table under Forward Fill Value.
At which index does the backward fill replace a missing value with 4?
AIndex 1
BIndex 5
CIndex 0
DIndex 4
💡 Hint
Look at execution_table rows 2 and 3 under Backward Fill Value.
If the first value in the column was NaN, what would forward fill do to it?
AFill it with zero
BFill it with the next valid value
CLeave it as NaN
DFill it with the last valid value
💡 Hint
Forward fill uses previous valid values; no previous value means no fill (see key_moments).
Concept Snapshot
Forward fill (ffill) replaces missing values with the last known valid value above.
Backward fill (bfill) replaces missing values with the next valid value below.
Use df.ffill() or df.bfill() in pandas.
Missing values at start (ffill) or end (bfill) may remain if no valid neighbor.
Useful for filling gaps in time series or ordered data.
Full Transcript
We start with a DataFrame that has missing values. Forward fill replaces each missing value with the previous valid value found above it in the column. Backward fill replaces missing values with the next valid value found below. For example, at index 1 and 2, forward fill uses the value 1 from index 0, while backward fill uses the value 4 from index 3. Missing values at the start cannot be forward filled because there is no previous value. This method helps fill gaps in data smoothly. The execution table shows each step's original and filled values. The variable tracker shows how the column changes after each fill method. Understanding these fills helps handle missing data in real datasets.