Distribution of Tickets Closed per Shift
Problem
You are given two DataFrames. `shift` has columns `agent_id` and `shift_day`; the pair is unique, and each row means an agent worked a shift on that day. `closure` has columns `agent_id`, `closed_on`, and `ticket_no`; each row records one support ticket an agent closed on a day. An agent can close several tickets on the same day, and may close tickets only on a day they actually worked a shift.
For every shift, count how many tickets the agent closed during it. Then build a histogram: for each possible number of tickets closed, report how many shifts had exactly that count.
Return `tickets_closed` (the count value, starting from 0) and `shift_count` (how many shifts closed that many tickets). Include every integer from 0 up to the maximum number of tickets closed in any single shift, even if `shift_count` is 0. Order by `tickets_closed`.
**Example**
```text
If the per-shift ticket counts are {0, 1, 1, 2}, the result is:
tickets_closed=0 -> 1 shift, tickets_closed=1 -> 2 shifts, tickets_closed=2 -> 1 shift.
```
Input data
Example rows — the live problem includes the full dataset.
| agent_id | shift_day |
|---|---|
| 1 | 2023-01-01 |
| 1 | 2023-01-02 |
| 2 | 2023-01-01 |
| 2 | 2023-01-02 |
| agent_id | closed_on | ticket_no |
|---|---|---|
| 1 | 2023-01-01 | 500 |
| 2 | 2023-01-01 | 501 |
| 2 | 2023-01-01 | 502 |
Expected output
Your answer should return 3 rows with the columns tickets_closed, shift_count.
Starter code (Pandas (Python))
import pandas as pd
def distribution_of_tickets_closed_per_shift(shift, closure) -> pd.DataFrame:
# Your code here
return shiftSolve this Pandas question free
Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock