How Often Each Host Was a Guest
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.
| episode_id | host_id | guest_id |
|---|---|---|
| 1 | 10 | 20 |
| 2 | 10 | 30 |
| 3 | 20 | 10 |
| 4 | 30 | 10 |
| 5 | 20 | 40 |
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 episodesSolve 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