From c19f808b3cd6c88253d6a4a672e5e5ae741fda60 Mon Sep 17 00:00:00 2001 From: FAQ Bot Date: Tue, 22 Sep 2026 06:47:36 +0000 Subject: [PATCH] NEW: Why are there still missing values after I use `fillna()` in Pandas? --- ...andas-fillna-missing-values-not-updated.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 _questions/machine-learning-zoomcamp/module-2/033_2885d25af1_pandas-fillna-missing-values-not-updated.md diff --git a/_questions/machine-learning-zoomcamp/module-2/033_2885d25af1_pandas-fillna-missing-values-not-updated.md b/_questions/machine-learning-zoomcamp/module-2/033_2885d25af1_pandas-fillna-missing-values-not-updated.md new file mode 100644 index 00000000..cef04c85 --- /dev/null +++ b/_questions/machine-learning-zoomcamp/module-2/033_2885d25af1_pandas-fillna-missing-values-not-updated.md @@ -0,0 +1,30 @@ +--- +id: 2885d25af1 +question: Why are there still missing values after I use `fillna()` in Pandas? +sort_order: 33 +--- + +By default, Pandas operations like `fillna()` don’t modify the original DataFrame in-place; they return a modified copy. If you don’t assign that result back, the missing values remain. + +Fix: assign the returned Series/column back, e.g.: +- `df['score'] = df['score'].fillna(fill_value)` + +Alternative: you can write into the existing data by using `inplace=True`, e.g.: +- `df['score'].fillna(fill_value, inplace=True)` + +Example: +```python +import pandas as pd +import numpy as np + +df = pd.DataFrame({"score": [100, np.nan, 150]}) +fill_value = 0 + +# INCORRECT: change is lost because the result isn't assigned +df["score"].fillna(fill_value) +print(df["score"].isnull().sum()) # 1 + +# CORRECT: reassign the column +df["score"] = df["score"].fillna(fill_value) +print(df["score"].isnull().sum()) # 0 +``` \ No newline at end of file