Sub topic 2
How Missing Values Affect Counts
This chapter assumes you know basic Python syntax, can build a pandas Series or DataFrame, and can select a column with df["name"]. It shows which counting methods skip missing values and which keep them. A wrong choice here changes your totals without raising an error.
What counts as missing
pandas stores a missing value as NaN (not a number), None, or pd.NA. Counting methods treat all of these as missing. Whether a method skips them is a default you can often change.
A small dataset
The examples use one DataFrame. The last row has no city.
import pandas as pd
df = pd.DataFrame({
"city": ["Oslo", "Rome", "Oslo", "Paris", "Rome", "Oslo", None],
"plan": ["free", "pro", "free", "pro", "pro", "pro", "free"],
})
print(len(df)) # 7 rows
print(df["city"].count()) # 6 non-missing entries
print(df["city"].isna().sum()) # 1 missing entrylen(df) counts every row. count() counts only the entries that are not missing. The difference between them is the number of missing values.
Which methods skip missing values
Most counting methods ignore missing values by default. Several accept dropna=False to include them.
| Method | Default behavior | How to include missing values |
|---|---|---|
| `Series.count()` | Skips missing values | Not possible, use `len()` instead |
| `value_counts()` | Skips missing values | `dropna=False` |
| `nunique()` | Skips missing values | `dropna=False` |
| `unique()` | Includes missing values | Always included |
| `groupby(...).size()` | Counts all rows | Always included |
print(df["city"].value_counts()) # Oslo 3, Rome 2, Paris 1
print(df["city"].value_counts(dropna=False)) # adds a row for the missing value, count 1
print(df["city"].nunique()) # 3
print(df["city"].nunique(dropna=False)) # 4
print(df["city"].unique()) # includes the missing valueThe default value_counts() result adds up to 6, not 7. If you report "share of all customers", a total of 6 is wrong, because one customer has no city.
Missing values inside groups
Grouped counts show the same split. size() counts rows, and count() counts non-missing values in a chosen column.
print(df.groupby("plan").size()) # free 3, pro 4
print(df.groupby("plan")["city"].count()) # free 2, pro 4The free group has 3 rows but only 2 known cities. If you use size() when you meant count(), you overstate how much data you have.
Common errors
- Percentages that do not add up.
normalize=Truedivides by the non-missing total unless you also passdropna=False. - Forgetting that
unique()returns the missing value. A list of "distinct cities" that containsNoneornancan break later string operations. - Treating empty strings as missing. The value
""is not missing. pandas counts it as a normal value, so convert it first withdf["city"].replace("", pd.NA).
Exercise
For the DataFrame above, df["city"].nunique() returns 3. What does df["city"].nunique(dropna=False) return, and why?
Show answer
It returns 4. With dropna=False, the missing value counts as a fourth distinct item alongside Oslo, Rome and Paris.