Count how often each distinct value appears in a pandas Series
Quick Overview
Write a pandas function that counts how often each distinct value appears in a Series, sorted from most to least frequent with missing values left out. It tests pandas frequency counting and its defaults, the pandas 2.x result naming, tie handling, and how a category dtype reports unused categories.
Count how often each distinct value appears in a pandas Series
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Write `def category_frequencies(s: pd.Series) -> pd.Series` that returns how many times each distinct value appears in a Series of categorical labels. The result index holds the distinct values and the values hold their integer counts.
The result must meet these requirements:
- Sort by count, highest first.
- Return raw integer counts, not proportions.
- Exclude missing values (`NaN` or `None`).
- The result Series is named `'count'`, as in pandas 2.x.
Example:
```python
s = pd.Series(['a', 'b', 'a', 'c', 'a', 'b', None])
category_frequencies(s)
# a 3
# b 2
# c 1
# Name: count, dtype: int64
```
```hint Check the defaults
pandas has a single Series method that builds a frequency table. Compare its default arguments with each requirement before you add any arguments of your own.
```
### What a Strong Answer Covers
- The method used, and which of its defaults satisfy each requirement.
- How missing values are treated.
- What the result's name and index look like, and how that differs across pandas versions.
- What happens to tied counts and to a Series with dtype `category`.
### Follow-up Questions
- How would you return proportions instead of raw counts?
- How would you include NaN in the counts?
- What changes if `s` has dtype `category` and some categories never appear in the data?
Overview: Write a pandas function that counts how often each distinct value appears in a Series, sorted from most to least frequent with missing values left out. It tests pandas frequency counting and its defaults, the pandas 2.x result naming, tie handling, and how a category dtype reports unused categories.
Write def category_frequencies(s: pd.Series) -> pd.Series that returns how many times each distinct value appears in a Series of categorical labels. The result index holds the distinct values and the values hold their integer counts.
The result must meet these requirements:
Sort by count, highest first.
Return raw integer counts, not proportions.
Exclude missing values (
NaN
or
None
).
The result Series is named
'count'
, as in pandas 2.x.
Example:
s = pd.Series(['a', 'b', 'a', 'c', 'a', 'b', None])
category_frequencies(s)
# a 3
# b 2
# c 1
# Name: count, dtype: int64
What a Strong Answer Covers Guidance
The method used, and which of its defaults satisfy each requirement.
How missing values are treated.
What the result's name and index look like, and how that differs across pandas versions.
What happens to tied counts and to a Series with dtype
category
.
Follow-up Questions Guidance
How would you return proportions instead of raw counts?
How would you include NaN in the counts?
What changes if
s
has dtype
category
and some categories never appear in the data?