AnalystPath

Classifying Departments in an Org Chart

PandasMediumMid level~10 min

Problem

A company stores its department hierarchy in the `orgunits` DataFrame. Each row has a department id `unit_id` and the id of the department it reports to, `reports_to`. The single top department reports to nobody, so its `reports_to` is NaN.

Classify every department into one column `position`:

- `Top` if the department reports to nobody.
- `Middle` if at least one other department reports to it (and it itself reports to someone).
- `Frontline` if no department reports to it.

Return `unit_id` and `position`, ordered by `unit_id` ascending.

Input data

Example rows — the live problem includes the full dataset.

orgunits
unit_idreports_to
1122
3322
6688
9988
2255

Expected output

Your answer should return 7 rows with the columns unit_id, position.

Starter code (Pandas (Python))

import pandas as pd

def classify_units(orgunits) -> pd.DataFrame:
    # Your code here
    return orgunits

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