Build a long-tail coverage report for a categorical column in pandas
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Implement `long_tail_report(s: pd.Series) -> pd.Series`, which describes the head and tail shape of a categorical Series through its coverage curve (levels ranked by descending count). Exclude `NaN` first. Return a `float64` Series named `long_tail` whose index is named `metric`.
The function must meet these requirements:
- The index order is exactly `n_levels, levels_for_50pct, levels_for_80pct, levels_for_95pct, n_singletons, tail_share`.
- `n_levels` is the number of distinct non-null levels. `n_singletons` is the number of levels that occur exactly once.
- `levels_for_50pct`, `levels_for_80pct` and `levels_for_95pct` are the minimum number of leading levels whose cumulative share (percent of non-null rows) reaches or exceeds 50, 80 and 95.
- `tail_share` is `100 - head_coverage`, where head coverage is the cumulative share of the `levels_for_80pct` head.
- All values are `float64`, including the counts.
Example:
```python
s = pd.Series(["a"] * 5 + ["b"] * 2 + ["c", "d", "e"] + [None, None])
long_tail_report(s)
# metric
# n_levels 5.0
# levels_for_50pct 1.0
# levels_for_80pct 3.0
# levels_for_95pct 5.0
# n_singletons 3.0
# tail_share 20.0
# Name: long_tail, dtype: float64
```
The two `None` values are excluded, leaving 10 rows. The ranked counts 5, 2, 1, 1, 1 give cumulative shares of 50, 70, 80, 90 and 100 percent. Level `a` alone reaches 50%, the top three reach exactly 80% (leaving a 20% tail), and all five are needed for 95%.
```hint Use the shape of the curve
Once the counts are sorted in descending order, the cumulative share never decreases. Think about what that lets you do for each threshold.
```
```hint Exact boundary hits
A head that lands exactly on 80% counts as reaching it. Make sure both your comparison and your arithmetic agree on that.
```
### Constraints & Assumptions
- `s` is a pandas Series of categorical values (strings, integers or a `category` dtype) and may contain missing values.
- Shares are percentages of the non-null rows only.
- The grader compares the result with `assert_series_equal` at float tolerance on a fixed long-tailed Series.
### Clarifying Questions to Ask
- For a `category` dtype, should categories that never occur count as levels?
- What should the report contain when the Series has no non-null values at all?
### What a Strong Answer Covers
- Dropping missing values and building counts sorted by descending frequency.
- Finding each `levels_for_*pct` value as the first position where cumulative coverage reaches the threshold, with equality included.
- Computing `tail_share` from the same 80% head, so the two metrics agree.
- The exact output contract: index order, index name, Series name and `float64` values.
- Edge cases such as exact boundary hits, unused categories and an all-NaN input.
### Follow-up Questions
- How would you handle ties in counts affecting `levels_for_50pct`?
- How would you extend this to report a Gini or entropy-based concentration measure?
- How would you produce the same report for a column with billions of rows that does not fit in memory?
Overview: Write a pandas function that summarizes how concentrated a categorical column is: how many top levels cover 50, 80 and 95 percent of rows, how many levels appear once, and the share left in the tail. It tests coverage curves, threshold boundaries and an exact output contract.