AnalystPath

Replies per Help-Desk Ticket

PandasEasyJunior level~10 min

Problem

You are given a DataFrame `messages` with columns `msg_id` and `root_id`. There is no single unique key; the same `msg_id` may appear more than once.

A message with `root_id` equal to NULL (missing) is a brand-new **ticket**. A message with a non-null `root_id` is a **reply**, and `root_id` is the `msg_id` of the ticket it replies to.

For each ticket, report how many **distinct** replies it has. A reply counts once even if its `msg_id` appears multiple times. A reply whose `root_id` does not match any ticket is ignored.

Return `ticket_id` and `reply_count`, ordered by `ticket_id` ascending.

Input data

Example rows — the live problem includes the full dataset.

messages
msg_idroot_id
1
2
1
12
31

Expected output

Your answer should return 3 rows with the columns ticket_id, reply_count.

Starter code (Pandas (Python))

import pandas as pd

def replies_per_ticket(messages) -> pd.DataFrame:
    # Your code here
    return messages

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