Fill missing values in Pandas

The “fillna” function in Pandas not only can replace missing values with a given constant value, like in this example:

1
2
3
4
import pandas as pd
import numpy as np
df = pd.DataFrame([[np.nan], [2], [np.nan], [0]])
df
A dataframe with missing values
A dataframe with missing values
1
df.fillna(47)
Missing values replaced with a constant
Missing values replaced with a constant

You can also replace a missing value with the next (or previous) value in the data frame!

1
df.fillna(method = "ffill")
Missing values filled with the previous existing value.
Missing values filled with the previous existing value.

Note that the first value cannot be replaced because nothing is preceding it.

Do you want to show your product/service to 25000 data science enthusiasts every month? I am looking for companies which would like to become a partner of this blog.

Are you interested? Is your employer interested? Here are the details of the offer.

You can also use the value of the next row to fill a missing value.

1
df.fillna(method = "bfill")
Missing values filled with the next existing value.
Missing values filled with the next existing value.

Remember to share on social media!
If you like this text, please share it on Facebook/Twitter/LinkedIn/Reddit or other social media.

If you watch programming live streams, check out my YouTube channel.
You can also follow me on Twitter: @mikulskibartosz

If you want to hire me, send me a message on LinkedIn or Twitter.


If this article was helpful, consider donating to WWF or any other charity of your choice.
Bartosz Mikulski
Bartosz Mikulski * data scientist / software engineer * conference speaker * organizer of School of A.I. meetups in Poznań * co-founder of Software Craftsmanship Poznan & Poznan Scala User Group