AnalystPath

Each Author's Home Library

PandasEasyJunior level~10 min

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.

membership
author_idlibrary_idhome_flag
11N
21Y
22N
33N
42N

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 membership

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