🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.fillna()

AI & DATA SCIENCE // df-fillna

df.fillna() replaces missing (NaN) values with a specified value, or by propagating a nearby existing value forward or backward.

Syntax

df.fillna(value=None, method=None)

Deep Dive Course

Passing a scalar value, or a dict mapping column names to different fill values, replaces every NaN with that value directly. The method parameter instead fills gaps using nearby data: forward-fill carries the last valid value forward into subsequent NaN positions, and backward-fill does the reverse, pulling the next valid value backward — both are common for time series data, where the most recent known value is often a reasonable stand-in for a missing one.

1Understanding df.fillna()

Passing a scalar value, or a dict mapping column names to different fill values, replaces every NaN with that value directly. The method parameter instead fills gaps using nearby data: forward-fill carries the last valid value forward into subsequent NaN positions, and backward-fill does the reverse, pulling the next valid value backward — both are common for time series data, where the most recent known value is often a reasonable stand-in for a missing one.

💡

Pass a dict to fillna(), mapping different column names to different fill values, to fill different columns with different, contextually appropriate values in a single call, instead of separate fillna() calls per column.

editor.html
import pandas as pd
import numpy as np

df = pd.DataFrame({"score": [80, np.nan, 90, np.nan]})
print(df.fillna(0))
localhost:3000

2Practical Example

Here is a real-world application of df.fillna() showing how it is used in production Pandas code.

editor.html
import pandas as pd
import numpy as np

df = pd.DataFrame({"temp": [70, np.nan, np.nan, 75]})
print(df.fillna(method="ffill"))
localhost:3000

3Best Practices

Follow these guidelines when working with df.fillna():

1. Fill numeric columns with a meaningful statistic, like the column's mean or median, rather than an arbitrary placeholder like 0, unless 0 is genuinely meaningful for that column

2. Use forward-fill/backward-fill specifically for time series or ordered data where a nearby value is a reasonable stand-in for a gap

3. Pass a dict to fill different columns with different appropriate values in one call, instead of separate fillna() calls per column

⚠️

Tip: Pass a dict to fillna(), mapping different column names to different fill values, to fill different columns with different, contextually appropriate values in a single call, instead of separate fillna() calls per column.

editor.html
import pandas as pd
import numpy as np

df = pd.DataFrame({"score": [80, np.nan, 90, np.nan]})
print(df.fillna(0))
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd
import numpy as np

df = pd.DataFrame({"score": [80, np.nan, 90, np.nan]})
print(df.fillna(0))
Example 02Advanced Example
import pandas as pd
import numpy as np

df = pd.DataFrame({"temp": [70, np.nan, np.nan, 75]})
print(df.fillna(method="ffill"))

Best Practices

  • Fill numeric columns with a meaningful statistic, like the column's mean or median, rather than an arbitrary placeholder like 0, unless 0 is genuinely meaningful for that column
  • Use forward-fill/backward-fill specifically for time series or ordered data where a nearby value is a reasonable stand-in for a gap
  • Pass a dict to fill different columns with different appropriate values in one call, instead of separate fillna() calls per column

Interview Question

Why might filling missing numeric values with a fixed 0 sometimes be worse than doing nothing at all?

Hint: Think about what a 0 would actually represent in the context of the data's meaning.

If a column represents something like age, income, or a test score, 0 isn't a neutral, harmless placeholder — it's a specific, meaningful value that can badly distort statistics computed over that column, dragging the mean sharply downward and misrepresenting what the missing entries actually should have looked like. Filling with a more representative value, like the column's mean or median, or leaving the value as NaN so it's properly excluded from calculations, is usually more honest about the data's real uncertainty than silently pretending a missing value was actually zero.

Exercises

MediumPractice using df.fillna() in a real scenario.
View Solution
import pandas as pd
import numpy as np

df = pd.DataFrame({"score": [80, np.nan, 90, np.nan]})
print(df.fillna(0))

Frequently Asked Questions

Why might filling missing numeric values with a fixed 0 sometimes be worse than doing nothing at all?

If a column represents something like age, income, or a test score, 0 isn't a neutral, harmless placeholder — it's a specific, meaningful value that can badly distort statistics computed over that column, dragging the mean sharply downward and misrepresenting what the missing entries actually should have looked like. Filling with a more representative value, like the column's mean or median, or leaving the value as NaN so it's properly excluded from calculations, is usually more honest about the data's real uncertainty than silently pretending a missing value was actually zero.

Related Functions

df-dropnadf-isnadf-replace