Side-by-Side Boxes

Data with PythonMedium+10 XP+10

Side-by-Side Boxes

Box plots are at their best side by side: one box per group, on the same scale. Before drawing them, a bus company wants the key numbers for each route.

Task: write boxes_by_group(groups, values). Journey i was on route groups[i] and took values[i] minutes.

Return a dictionary with one key per route. Each value is a list [median, iqr, outliers] for that route's journeys only:

  • median and iqr are worked out the way pandas quantile does it. iqr is Q3 − Q1. Both are floats rounded to 2 decimal places.
  • outliers is how many of that route's journeys are below Q1 − 1.5 × IQR or above Q3 + 1.5 × IQR, using the route's own quartiles. It's an int.

Every route has at least one journey, and all the times are whole numbers.

Examples

groups = ["A", "A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B"]
values = [12, 14, 15, 15, 16, 18, 19, 21, 95, 20, 22, 25, 30]
returns {'A': [16.0, 4.0, 1], 'B': [23.5, 4.75, 0]}
groups = ["7", "9", "7", "9", "7", "9"]
values = [30, 28, 34, 29, 31, 60]
returns {'7': [31.0, 2.0, 0], '9': [29.0, 16.0, 0]}

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run