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
normalize=Trueturns counts into shares.dropna=Falsecounts the gaps as their own group.uniqueandnuniqueanswer the other two obvious questions.
Keywords and builtins used here
asprintsorted
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.
Step 4 of 5 in Grouping, step 17 of 26 in pandas.