liveBook · Manning

Value_counts

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

A pandas method that counts the occurrences of each distinct value in a one-dimensional labeled data structure.

`value_counts()` answers a simple data question: which distinct values occur, and how often? Called on a pandas Series, it returns a new Series whose index holds the distinct input values and whose integer values hold their frequencies. Results are ordered from most common to least common, making a frequency table immediately useful for inspection. section 1.23 section 5.14

Count taxi passenger totals

After loading passenger counts into `s`, use `s.value_counts()` rather than writing a separate mask and `count()` for every possible total. The returned Series can then be indexed to retrieve particular counts, such as one- and six-passenger rides. section 1.23

s.value_counts(normalize=True)[[1,6]]

Set `normalize=True` when the question is about shares rather than raw totals. The resulting frequency Series contains fractions, so selecting its labels gives the proportion for each requested value. section 1.23

Because the result is itself a Series, continue analyzing it with familiar Series operations. Use `head()` to retain only the most frequent values, or select labels from its index to focus on specific categories. This pattern works after first selecting a DataFrame column, including a column chosen from rows filtered through an index. section 1.23 section 4.2

Find the most ticketed vehicle makes

With `Issue Date` as the index, select one date and the `Vehicle Make` column, then chain `.value_counts().head(3)`. The output ranks the three makes receiving the most tickets on that date. The same shape of query finds the most-ticketed streets or the leading make among selected vehicle colors. section 4.2

Frequency tables are also a practical data-cleaning diagnostic. A large number of distinct values, or many spelling variants among the most common values, can reveal inconsistent labels. Count before cleaning, replace known variants with a translation dictionary, and count again to check whether standardization reduced the number of categories. section 5.14 section 5.16

Prepare values before counting when different spellings should mean the same thing. For text, lowercasing and stripping punctuation before `value_counts()` prevents sentence capitalization and edge punctuation from splitting otherwise identical words. For numeric measurements, use `pd.cut` to create labeled bins, then count the resulting categories. section 9.2 section 1.27

Useful follow-up patterns include:

  • Use `.value_counts(normalize=True)` for proportions instead of raw counts. section 1.23
  • Use `.value_counts().head(n)` to report the most common `n` values. section 4.2 section 8.2
  • Divide frequencies for a filtered subset by frequencies for the full population to compare subgroup shares by category. section 8.2

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

How deep it goes
Explains the returned Series, descending ordering, normalization, indexing, chaining, and preprocessing before counting.
Examples
Uses taxi passengers and distances, parking-ticket attributes, survey responses, and cleaned text-word frequencies.
What it assumes
Assumes ability to select Series or DataFrame columns and benefits from familiarity with indexing and method chaining.
What you can do after
Apply it via pandas Series methods to rank values, calculate proportions, inspect inconsistent labels, and count prepared categories.
How it connects
Connects Series selection, indexes, method chaining, pd.cut, string accessors, replace, and data cleaning.

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.