Write a pandas function that averages a metric per segment and returns the k highest and k lowest segments with their ranks. It tests groupby aggregation, sorting in two directions with a consistent tie-break, and producing exact column dtypes and a clean index.
Write `top_and_bottom_segments(df, seg, metric, k)` that computes the mean of `metric` per `seg` and returns exactly `2*k` rows: the `k` highest-mean segments, then the `k` lowest-mean segments. Assume more than `2*k` segments exist.
The output must follow these rules:
- Columns are `rank_type` (object, `'top'` or `'bottom'`), `rank` (int64), `segment` (object) and `segment_mean` (float64), in that order.
- The `'top'` block comes first, ordered by `segment_mean` descending. The `'bottom'` block follows, ordered by `segment_mean` ascending.
- `rank` runs from 1 to `k` within each block. Rank 1 is the highest mean in the top block and the lowest mean in the bottom block.
- Mean ties are broken by `segment` ascending in both blocks.
- The final index is a `RangeIndex` from 0 to `2*k - 1`.
- The result is checked with `assert_frame_equal` on a fixed 6-segment frame with `k=2`, so dtypes and row order count.
With segment means A=10, B=9, C=8, D=7, E=6, F=5 and `k=2`, the result is:
```text
rank_type rank segment segment_mean
0 top 1 A 10.0
1 top 2 B 9.0
2 bottom 1 F 5.0
3 bottom 2 E 6.0
```
A and B have the two highest means. The bottom block starts from the lowest mean, so F is rank 1 and E is rank 2.
```hint Watch the tie-break direction
The means sort in opposite directions in the two blocks, but the segment tie-break is ascending in both. Check whether one sorted list read from both ends can satisfy that.
```
### What a Strong Answer Covers
- Aggregating to one mean per segment before ranking.
- Building each block with its own sort and the ascending segment tie-break.
- Ranks from 1 to `k` in each block, with the right meaning of rank 1 in each.
- The exact column order, dtypes and a fresh `RangeIndex`.
### Follow-up Questions
- What if fewer than `2*k` segments exist and the top and bottom blocks overlap?
- How would you keep a segment with only a handful of rows from reaching the top of the list on noise alone?
- How should the function treat rows with a missing segment label, or a segment whose metric is missing in every row?
Overview: Write a pandas function that averages a metric per segment and returns the k highest and k lowest segments with their ranks. It tests groupby aggregation, sorting in two directions with a consistent tie-break, and producing exact column dtypes and a clean index.
Write top_and_bottom_segments(df, seg, metric, k) that computes the mean of metric per seg and returns exactly 2*k rows: the k highest-mean segments, then the k lowest-mean segments. Assume more than 2*k segments exist.
The output must follow these rules:
Columns are
rank_type
(object,
'top'
or
'bottom'
),
rank
(int64),
segment
(object) and
segment_mean
(float64), in that order.
The
'top'
block comes first, ordered by
segment_mean
descending. The
'bottom'
block follows, ordered by
segment_mean
ascending.
rank
runs from 1 to
k
within each block. Rank 1 is the highest mean in the top block and the lowest mean in the bottom block.
Mean ties are broken by
segment
ascending in both blocks.
The final index is a
RangeIndex
from 0 to
2*k - 1
.
The result is checked with
assert_frame_equal
on a fixed 6-segment frame with
k=2
, so dtypes and row order count.
With segment means A=10, B=9, C=8, D=7, E=6, F=5 and k=2, the result is:
rank_type rank segment segment_mean
0 top 1 A 10.0
1 top 2 B 9.0
2 bottom 1 F 5.0
3 bottom 2 E 6.0
A and B have the two highest means. The bottom block starts from the lowest mean, so F is rank 1 and E is rank 2.
What a Strong Answer Covers Guidance
Aggregating to one mean per segment before ranking.
Building each block with its own sort and the ascending segment tie-break.
Ranks from 1 to
k
in each block, with the right meaning of rank 1 in each.
The exact column order, dtypes and a fresh
RangeIndex
.
Follow-up Questions Guidance
What if fewer than
2*k
segments exist and the top and bottom blocks overlap?
How would you keep a segment with only a handful of rows from reaching the top of the list on noise alone?
How should the function treat rows with a missing segment label, or a segment whose metric is missing in every row?