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

1
df.fillna(47)

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

Note that the first value cannot be replaced because nothing is preceding it.
Parsing machine learning logs with Ahana, a managed Presto service, and Cube, a headless BI solution

Check out my article published on the Cube.dev blog!
You can also use the value of the next row to fill a missing value.
1
df.fillna(method = "bfill")

You may also like
Bartosz Mikulski
- Data/MLOps engineer by day
- DevRel/copywriter by night
- Python and data engineering trainer
- Conference speaker
- Contributed a chapter to the book "97 Things Every Data Engineer Should Know"
- Twitter: @mikulskibartosz