Sub topic 3
Counting non-null values with count
This chapter assumes you can build a pandas Series and DataFrame and select a column from a DataFrame. It also assumes you know that pandas stores a missing value as NaN or None.
What count returns
count returns the number of entries that are not missing. It does not count rows, and it does not count distinct values. A missing entry is skipped, so count is the quickest check of how complete a column is.
The examples in this chapter 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(df["city"].count()) # 6
print(len(df["city"])) # 7len returns 7 because it counts every position. count returns 6 because it leaves out the None in the last row.
Counting a whole DataFrame
Called on a DataFrame, count returns one number per column.
print(df.count())city 6
plan 7
dtype: int64The result is a Series indexed by column name. Comparing it with len(df) shows which columns have gaps.
missing_per_column = len(df) - df.count()
print(missing_per_column) # city 1, plan 0df.isna().sum() gives the same answer. It counts the missing entries directly, while count counts the present ones.
Counting across rows
Pass axis=1 to count the non-missing values in each row. This is useful for finding rows that are mostly empty.
print(df.count(axis=1)) # 2 for rows 0 to 5, 1 for row 6Common errors
The most frequent mistake is using count when you want a row total. If a column has gaps, count returns less than the number of rows. Use len(df) or df.shape[0] for the row total.
A second mistake is expecting count to tell you how many different values appear. For the city column it returns 6, but there are only 3 distinct cities. nunique answers that question, and a later chapter covers it.
The third mistake is treating count as a boolean-style tally. Series.count does not count True values. A column of True and False still returns the number of entries that are present. To count the True values, call sum on the column.
count and the groupby methods
count follows the same rule inside a group. df.groupby("plan")["city"].count() returns the non-missing cities in each plan. df.groupby("plan").size() counts every row, missing values included. The groupby chapter covers that difference in detail.
Choosing between len and count
| Question | Use |
|---|---|
| How many rows are there? | `len(df)` |
| How many entries are present in this column? | `df["city"].count()` |
| How many entries are missing? | `df["city"].isna().sum()` |
| How many present entries per column? | `df.count()` |
Exercise
A Series is built with pd.Series([4, None, 7, None, 9]). What do s.count(), len(s), and s.isna().sum() return?
Show answer
s.count() returns 3, because only 4, 7 and 9 are present. len(s) returns 5, because it counts every position. s.isna().sum() returns 2, the number of missing entries.