AnalystPath

Interleave Volunteers by Shift Type

PandasMediumMid level~10 min

Problem

A community kitchen tracks its volunteers and which shift type each one prefers. The DataFrame `roster` (loaded from `roster.csv`) has columns `(volunteer_id, shift)` where `shift` is one of `'morning'`, `'evening'`, or `'overnight'`.

Reorder the rows so the shifts cycle in the fixed order `'morning'`, then `'evening'`, then `'overnight'`, repeating. Inside each shift, list volunteers by `volunteer_id` ascending. So the first three rows are the lowest-id morning, evening, and overnight volunteers; the next three are the second-lowest of each shift; and so on. If a shift runs out of volunteers, simply skip it for the remaining cycles.

Return the columns `volunteer_id` and `shift` in this interleaved order.

Input data

Example rows — the live problem includes the full dataset.

roster
volunteer_idshift
4overnight
7morning
2evening
5overnight
3morning

Expected output

Your answer should return 9 rows with the columns volunteer_id, shift.

Starter code (Pandas (Python))

import pandas as pd

def interleave_volunteers_by_shift(roster) -> pd.DataFrame:
    # Your code here
    return roster

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