Sub topic 5
Finding missing values with non-null counts
This chapter assumes you can run basic Python, have pandas imported as pd, and know that a DataFrame is a table of rows and labelled columns. It also assumes you have seen head and info called before. Here you will learn to read the non-null counts that info reports and to turn them into a list of columns with missing data.
What a non-null count tells you
A non-null count is the number of entries in a column that hold a real value. Pandas stores a missing entry as NaN (not a number) or None, and it excludes those entries from the count. If a DataFrame has 5 rows and a column has 4 non-null entries, one entry in that column is missing.
The info method prints this count for every column. Start with the small DataFrame used throughout this tutorial:
import numpy as np
import pandas as pd
df = pd.DataFrame({
"name": ["Ana", "Ben", "Cleo", "Dev", "Eli"],
"age": [34, 28, np.nan, 45, 39],
"city": ["Lima", "Oslo", "Lima", "Pune", "Oslo"],
"salary": [52000, 48000, 61000, 75000, 58000],
})
df.info()The output looks like this (the exact object or str label and the memory figure depend on your pandas version):
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 name 5 non-null object
1 age 4 non-null float64
2 city 5 non-null object
3 salary 5 non-null int64Compare each non-null count with the entry count in the second line, 5 entries. The age column shows 4 non-null, so one age is missing. The other columns are complete.
Reading the comparison
The method is a two-step check:
- Read the total from the
RangeIndexline. - Compare it with each column's non-null count. Any column below the total has missing values.
flowchart TDNote that age is float64, not int64. Integers cannot hold NaN in a default NumPy-backed column, so pandas converts the whole column to floats when one value is missing. A float dtype on a column that should hold whole numbers is often a sign of missing data.
Counting missing values directly
Subtracting by eye gets tedious with dozens of columns. These calls give you the numbers directly:
print(df.isna().sum()) # missing count per column
print(len(df) - df.count()) # same result: total minus non-null
print((df.isna().mean() * 100).round(1)) # percent missing per column
print(df[df.isna().any(axis=1)]) # rows with at least one missing valueisna() returns a table of True and False values. sum() counts the True values in each column, and mean() gives their share. count() is the non-null count that info prints. Percentages help you decide what to do next. A column that is 2% missing is a small repair, while a column that is 80% missing may not be worth keeping.
Missing values that info cannot see
Pandas counts only true missing markers. Placeholders such as an empty string, "N/A", or -1 count as real values:
raw = pd.DataFrame({"city": ["Lima", "", "N/A", None]})
print(raw["city"].isna().sum()) # 1
clean = raw.replace({"": np.nan, "N/A": np.nan})
print(clean["city"].isna().sum()) # 3Here info would report 3 non-null entries, but only one entry is truly absent. After you convert the placeholders, the count becomes 3 missing. To find placeholders, look at value_counts() for suspicious repeated values, or at describe for a minimum such as -1 or 0 in a column where that makes no sense. If the data comes from a file, pd.read_csv(..., na_values=["N/A", ""]) converts placeholders as it loads.
Common errors
- Reading
non-nullas "number of missing". The count is of present values, so the missing count is the total minus that number. - Assuming
infoshows percentages. It shows only counts, so compute shares withisna().mean(). - Wrapping
infoinprint. It prints by itself and returnsNone, soprint(df.info())adds a strayNoneline. - Forgetting that
infotruncates for very wide tables. Passshow_counts=Trueorverbose=Trueif the count column is missing from the output.
Exercise
A DataFrame has 1,200 entries. info reports 1,176 non-null for the column "income", and the column dtype is float64 even though incomes are whole numbers. How many values are missing, what share is that, and why is the dtype float64?
Show answer
1,200 - 1,176 = 24 values are missing, which is 24 / 1,200 = 2%. The dtype is float64 because a default NumPy-backed integer column cannot store NaN, so pandas converts the column to floats when it holds missing values.