Reference solution
The shape we check against. Any implementation that passes the assertions is valid — this one favours clarity.
def drama_events(df):
return df.loc[df['genre'] == 'drama', ['event_id', 'user', 'minutes']]
The DataFrame `watch_events` is preloaded. Write drama_events(df) that returns the rows where genre == 'drama', keeping only event_id, user, and minutes (in that order).
preloaded fixtures
import pandas as pd
import numpy as np
watch_events = pd.DataFrame({
"event_id": range(1, 13),
"user": ["alice", "bob", "alice", "carol", "bob", "alice", "carol", "dave", "bob", "carol", "dave", "alice"],
"genre": ["drama", "comedy", "drama", "docs", "comedy", "thriller", "docs", "drama", "comedy", "thriller", "drama", "drama"],
"watch_date": pd.to_datetime(["2024-01-05", "2024-01-06", "2024-01-10", "2024-01-12", "2024-01-15", "2024-01-20", "2024-02-01", "2024-02-03", "2024-02-10", "2024-02-15", "2024-02-20", "2024-02-28"]),
"minutes": [45, 30, 60, 20, 25, 50, 15, 55, 40, 35, 60, 50],
})
hint ladder
Inspect a chained expression (df.query(…).groupby(…).agg(…)) to see the shape and preview after each step.
Run your code to see its output, or check your solution to grade it.
Reference solution
The shape we check against. Any implementation that passes the assertions is valid — this one favours clarity.
def drama_events(df):
return df.loc[df['genre'] == 'drama', ['event_id', 'user', 'minutes']]
Code is blurred until you solve this problem — the reasoning stays readable.