Each Author's Home Library
Problem
A library network records which libraries each author has borrowing rights at. A `membership` DataFrame has `author_id`, `library_id` and `home_flag` (one of `'Y'`, `'N'`).
When an author belongs to more than one library, exactly one row is flagged `'Y'` as the home library. When an author belongs to only one library, that single row is flagged `'N'` but it is still their home library.
Return each author together with their home `library_id`, in any order.
Input data
Example rows — the live problem includes the full dataset.
| author_id | library_id | home_flag |
|---|---|---|
| 1 | 1 | N |
| 2 | 1 | Y |
| 2 | 2 | N |
| 3 | 3 | N |
| 4 | 2 | N |
Expected output
Your answer should return 4 rows with the columns author_id, library_id.
Starter code (Pandas (Python))
import pandas as pd
def home_library(membership: pd.DataFrame) -> pd.DataFrame:
# Your code here
return membershipSolve 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