AnalystPath

Couriers per Depot

PandasEasyJunior level~10 min

Problem

You are given a DataFrame `courier` with columns `courier_id` and `depot_id`. `courier_id` is unique, and each row says which depot a courier works out of.

For each courier, report how many couriers in total belong to that courier's depot (the courier themselves included). Return `courier_id` and the count under the column `depot_headcount`. Rows may be in any order.

**Example**

```text
courier:
+------------+----------+
| courier_id | depot_id |
+------------+----------+
| 11 | 1 |
| 12 | 1 |
| 13 | 1 |
| 21 | 2 |
+------------+----------+

Result:
+------------+-----------------+
| courier_id | depot_headcount |
+------------+-----------------+
| 11 | 3 |
| 12 | 3 |
| 13 | 3 |
| 21 | 1 |
+------------+-----------------+
```

Depot 1 has three couriers, so each of them reports 3. Depot 2 has a single courier, who reports 1.

Input data

Example rows — the live problem includes the full dataset.

courier
courier_iddepot_id
111
121
131
212

Expected output

Your answer should return 4 rows with the columns courier_id, depot_headcount.

Starter code (Pandas (Python))

import pandas as pd

def couriers_per_depot(courier) -> pd.DataFrame:
    # Your code here
    return courier

Solve 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

Related Pandas questions