AnalystPath

How Often Each Host Was a Guest

PandasMediumMid level~10 min

Problem

You are given one DataFrame `episodes` with columns `episode_id`, `host_id`, and `guest_id`. `episode_id` is unique; `host_id` is the person running the episode and `guest_id` is the person invited onto it.

For each distinct person who has hosted at least one episode, return their id as `host_id` and `appearances`: the number of episodes in which that person appeared as a guest (0 if they were never a guest).

Return the result in any order.

Input data

Example rows — the live problem includes the full dataset.

episodes
episode_idhost_idguest_id
11020
21030
32010
43010
52040

Expected output

Your answer should return 3 rows with the columns host_id, appearances.

Starter code (Pandas (Python))

import pandas as pd

def host_guest_appearances(episodes) -> pd.DataFrame:
    # Your code here
    return episodes

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