AnalystPath

Shoppers With Multiple Products in a Day

PandasMediumMid level~10 min

Problem

You are given a DataFrame `scans` loaded from `scans.csv` with the columns `product_id`, `brand_id`, `shopper_id` and `scan_day`. There is no single-column key; each row means `shopper_id` scanned product `product_id` on `scan_day`.

Find every shopper who scanned **more than one distinct product on the same day**. Return one row per such shopper in a single column named `id`, sorted by `id` in ascending order.

Example `scans`:

```text
product_id brand_id shopper_id scan_day
10 1 4 2024-04-01
20 2 4 2024-04-01
30 1 7 2024-04-01
30 1 7 2024-04-02
```

Expected result is `[4]`: shopper 4 scanned two different products (10 and 20) on 2024-04-01. Shopper 7 only ever scanned product 30, so they do not qualify.

Input data

Example rows — the live problem includes the full dataset.

scans
product_idbrand_idshopper_idscan_day
10142024-04-01
20242024-04-01
30172024-04-01
30172024-04-02

Expected output

Your answer should return 1 row with the columns id.

Starter code (Pandas (Python))

import pandas as pd

def multi_product_shoppers(scans) -> pd.DataFrame:
    # Your code here
    return scans

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