typestar

Counting values in Python

value_counts is the first thing to run on any categorical column.

import pandas as pd

kinds = pd.Series(["snippet", "example", "snippet", None, "script",
                   "snippet"])

print(kinds.value_counts())
print(kinds.value_counts(normalize=True).round(2))
print(kinds.value_counts(dropna=False))
print(kinds.nunique(), sorted(kinds.dropna().unique()))

How it works

  1. normalize=True turns counts into shares.
  2. dropna=False counts the gaps as their own group.
  3. unique and nunique answer the other two obvious questions.

Keywords and builtins used here

The run, in numbers

Lines
9
Characters to type
276
Tokens
90
Three-star pace
110 tpm

At the three-star pace of 110 tokens a minute, this run takes about 49 seconds.

Type this snippet

Step 4 of 5 in Grouping, step 17 of 26 in pandas.

← Previous Next →