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

df.fillna(47)

You can also replace a missing value with the next (or previous) value in the data frame!
df.fillna(method = "ffill")

Note that the first value cannot be replaced because nothing is preceding it.
Want to build AI systems that actually work?
Download my expert-crafted GenAI Transformation Guide for Data Teams and discover how to properly measure AI performance, set up guardrails, and continuously improve your AI solutions like the pros.
You can also use the value of the next row to fill a missing value.
df.fillna(method = "bfill")
