---
title: "Fill Missing Values in Pandas: Complete Guide to Data Cleaning Techniques"
description: "How to handle missing data in pandas DataFrames"
author: "Bartosz Mikulski"
author_bio: "Principal AI Engineer & MLOps Architect. I bridge the gap between \"it works in a notebook\" and \"it works for 200 million users.\""
author_url: https://mikulskibartosz.name
author_linkedin: https://www.linkedin.com/in/mikulskibartosz/
author_github: https://github.com/mikulskibartosz
canonical_url: https://mikulskibartosz.name/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:

```
import pandas as pd
import numpy as np
df = pd.DataFrame([[np.nan], [2], [np.nan], [0]])
df
```

![A dataframe with missing values](/images/2018-09-19-fill-missing-values-in-pandas/dataframe.png)

```
df.fillna(47)
```

![Missing values replaced with a constant](/images/2018-09-19-fill-missing-values-in-pandas/replace_with_constant.png)

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.](/images/2018-09-19-fill-missing-values-in-pandas/fill_with_previous.png)

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

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.](/images/2018-09-19-fill-missing-values-in-pandas/fill_with_next.png)