How to forward fill in Python
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
The Pandas library will give you the tools to forward fill the NaN values.
What is forward fill ?
Forward fill will take for every NaN the past available value.
This useful when you are working with timeseries and need to take the latest available values.

Using fillna
The .fillna() method will replace all NaN or null values contained in a pandas.Series ou pandas.DataFrame.
As we could have seen in the fillna article we can specify the method we want to use.
Here for our example we are going to use the forward fill method or "ffill"
Using ffill on a Serie
# Here we apply the ffill method on a our column called my_column
df["my_column"] = df["my_column"].fillna(method="ffill")
Using ffill on a DataFrame
# Here we apply the ffill method on a our dataframe
df = df.fillna(method="ffill")
Here you are, you know how to replace your NaN by the last available value, or forward fill.
If you want to learn more about Backfill or Forward Fill, you can check the links below
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
Related Articles
Continue your learning journey with these related topics
Master Data Science in Days, Not Months 🚀
Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚