Skip to content
Hands-on Mentor
ENTR
Back to blog

pandas missing values: find, drop, or fill them

Missing values quietly distort an analysis. Learn how to find them with isna, remove them with dropna, fill them with fillna, and decide which one fits.

4 min read

Missing values in pandas are cells with no known value, usually shown as NaN. You can do three things with them: find them with isna(), drop the rows with dropna(), or fill them with a reasonable value using fillna(). The right choice depends on why the values are missing and how much data you would lose.

What does a table with missing values look like?

Here are eight customers from Bazaar, a fictional online store. The ages come from the real Bazaar customer file. I removed one city value to show a text gap too:

import pandas as pd
import numpy as np

customers = pd.DataFrame({
    "customer_id": ["M0001", "M0002", "M0003", "M0004", "M0005", "M0006", "M0007", "M0008"],
    "city": ["Kingsbridge", "Port Alder", None, "Bayview", "Bayview", "Kingsbridge", "Ironford", "Port Alder"],
    "age": [52, 32, 19, 46, np.nan, 36, np.nan, 44],
    "channel": ["organic", "social media", "referral", "organic", "organic", "referral", "referral", "social media"],
})

How do you check for null values in pandas?

Chain isna() with sum() to count the missing values in each column, or with mean() to get the share:

print(customers.isna().sum())
print(customers.isna().mean().round(3))
columnmissing countmissing share
customer_id00.000
city10.125
age20.250
channel00.000

isna() gives True or False for every cell. Python counts True as 1, so sum() returns the count and mean() returns the share. Always do this first. A quarter of the ages is missing, which is too much to ignore.

What does dropna do?

dropna() removes every row that has at least one missing value:

print(len(customers.dropna()))

The result is 5. Only 5 of 8 customers remain. No single column is missing more than a quarter, but the gaps are in different rows, so you lose three customers.

To drop rows only when one specific column is missing, use subset:

print(customers.dropna(subset=["age"]).shape)

The shape is (6, 4): only the two rows without an age are gone.

Dropping is fine when few rows are missing and the gaps are random. If the gaps have a cause, for example younger customers skip the age question more often, dropping makes the rest of your data biased. The average age of the remaining customers would look higher than it really is.

What is the difference between fillna and dropna?

dropna removes rows. fillna keeps the rows and puts a value in the gap. For a number column, you can fill with the mean or the median:

print(customers["age"].mean(), customers["age"].median())
print(customers["age"].fillna(customers["age"].median()).tolist())

The mean age is 38.17 and the median is 40.0. The median is usually the safer choice because extreme values affect it less.

For a text column, fill with the most common value or an explicit label:

print(customers["city"].fillna("Unknown").tolist())

The label "Unknown" keeps the gap visible. Later you can check whether that group behaves differently from the others.

fillna returns a new column and does not change the table. To keep the result, assign it back: customers["city"] = customers["city"].fillna("Unknown").

How do you handle missing data in Python?

Use this table to choose:

SituationGood choice
Very few missing (1-2%), randomDrop the rows
Number column, moderate gapsFill with the median and note it
Text columnFill with an "Unknown" label
The gap itself means somethingAdd a separate "is missing" column
Most of the column is missingConsider leaving the column out

What does filling cost you?

Filling with the mean keeps the mean the same but makes the data less spread out, because every filled value sits exactly at the center. Add a flag column first, then compare the standard deviations:

customers["age_missing"] = customers["age"].isna()
filled = customers["age"].fillna(customers["age"].mean())

print(round(customers["age"].std(), 2), round(filled.std(), 2))
print(customers["age_missing"].sum())

The standard deviation drops from 11.81 to 9.98. Your customers now look more alike than they are. Every value you fill is a guess you wrote yourself. The age_missing column records which values are guesses, and your report should say how many values you filled and how.

What mistakes do people make with missing data?

  • Never checking for gaps. mean() skips missing values without a warning. The 38.17 above is the mean of only six ages.
  • Dropping without thinking. One call to dropna() can remove most of your data.
  • Filling with 0. Counting an unknown age as 0 pulls the mean down.
  • Missing a gap that is not NaN. An empty string or a dash is a normal value for pandas, and isna does not see it:
raw = pd.Series([30, "", "-", 41])
print(raw.isna().sum())
print(raw.replace({"": np.nan, "-": np.nan}).isna().sum())

The first count is 0. After replacing the empty string and the dash with NaN, it is 2.

What to read next

  • [Missing values](/learn/statistics-data-analysis/missing-values): find the gaps in the full Bazaar data and choose a method for each column.
  • [Data quality](/learn/statistics-data-analysis/data-quality): the check that comes before cleaning.
  • [SQL NULL values](/blog/sql-null-values): the same problem on the SQL side.
  • [Statistics and Data Analysis course](/courses/statistics-data-analysis): 44 free lessons.
  • #pandas
  • #missing data
  • #data cleaning
  • #python
All posts