liveBook · Manning

Count

As explained in Pandas Workout: 200 exercises to make you a stronger data analyst

count is a pandas operation that returns the number of non-missing values in an object or along an axis.

`count` measures populated data: it returns the number of non-NaN values. On a Series, the result is one integer; on a DataFrame, it is a Series whose index is the original column names and whose values are each column’s non-missing counts. That makes it useful both for checking completeness and for answering “how many matching records?” after selecting rows. section 3.2 section 8.2

Count selected taxi rides

Build a boolean condition, use `loc` to select matching rows and one relevant column, then call `count()`. For example, selecting `passenger_count > 8` and counting `passenger_count` reports nine January 2019 rides. The selected column matters: `count()` excludes missing values, so it is most reliable as a row count when that column is populated for the records of interest. section 3.2

df = DataFrame([[10, 20, np.NaN, 40],
                [50, np.NaN, np.NaN, np.NaN],
                [np.NaN, 60, 70, 80]],
  index=list('abc'),
  columns=list('wxyz'))
df.count()

Run this small DataFrame example to see columnwise results: missing entries reduce each column’s count independently, producing a Series rather than one DataFrame-wide total. section 3.2

Use `count` in progressively broader analytical patterns:

  • Compare periods by filtering each year, counting a selected column, and subtracting the results. section 3.6
  • Measure response totals across many survey columns, then sort the count Series to identify the most common reported languages. section 8.2
  • Count records within groups by grouping on one or more keys, counting a nongrouping identifier, then sorting or taking the largest result. section 13.3
  • Choose `len(df.index)` or `size` when the goal is every row or value including NaN; `count` intentionally answers a different question. section 5.2

`count` also supports descriptive statistics: `describe()` includes the non-NaN count alongside mean, standard deviation, quantiles, and extrema. In calculations such as a mean or population standard deviation, the same count supplies the denominator for the available values. section 1.15 section 1.5

How Pandas Workout: 200 exercises to make you a stronger data analyst covers it

How deep it goes
Defines Series and DataFrame return shapes, then works through filtering, sorting, grouping, and missing-value limitations without implementation internals.
Examples
Uses New York taxi trips, developer-survey language responses, university records, and a small NaN-filled DataFrame.
What it assumes
Assumes familiarity with Series, DataFrames, boolean masks, loc selection, and basic grouping in applied queries.
What you can do after
Apply it via pandas to count non-missing selected values, compare subsets, and rank group totals.
How it connects
Connects boolean masking, loc, groupby, value_counts, describe, sum, size, and len to counting tasks.

Also explained in

Compare how 5 books explain count →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pandas Workout: 200 exercises to make you a stronger data analyst. The section links point to the parts of the book it draws on.