AnalystPath

Subscribers With Two Streams Within Five Days

PandasMediumMid level~10 min

Problem

You are given one DataFrame `streams` with columns `stream_id`, `listener_id`, and `played_on` (a date). `stream_id` is unique.

Return the `listener_id` of every listener who has any two streams that occurred at most 5 days apart (the two dates differ by 5 days or fewer).

Return the result ordered by `listener_id` ascending.

Input data

Example rows — the live problem includes the full dataset.

streams
stream_idlistener_idplayed_on
11002024-01-01
21002024-01-04
32002024-01-01
42002024-01-20
53002024-02-01

Expected output

Your answer should return 2 rows with the columns listener_id.

Starter code (Pandas (Python))

import pandas as pd

def streams_within_five_days(streams) -> pd.DataFrame:
    # Your code here
    return streams

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