The “fillna” function in Pandas not only can replace missing values with a given constant value, like in this example:
import pandas as pd
import numpy as np
df = pd.DataFrame([[np.nan], [2], [np.nan], [0]])
df
A dataframe with missing values
Missing values replaced with a constant
You can also replace a missing value with the next (or previous) value in the data frame!
df.fillna(method = "ffill")
Missing values filled with the previous existing value.
Note that the first value cannot be replaced because nothing is preceding it.
Subscribe to the newsletter
I help fintech teams stop AI hallucinations before they spark compliance nightmares or customer churn.
Why join?
- Straight-to-the-point articles on measuring and fixing hallucinations.
- Playbooks for advanced RAG tuning, guardrails, and evaluation you can copy-paste.
- Case studies from real deployments, not "Hello World" demos.
What to expect:
- 3-4 actionable emails per month.
- Proven expertise: 500+ articles & 1k+ engineers trained.
- Zero fluff, zero spam — just tactics that make your LLM stack safer.
Subscribe
You can also use the value of the next row to fill a missing value.
df.fillna(method = "bfill")
Missing values filled with the next existing value.