AnalystPath

Courses Booked Heavily in Two Consecutive Years

PandasMediumMid level~10 min

Problem

An online learning platform records course enrolments in the DataFrame `bookings` (loaded from `bookings.csv`) with columns `(booking_id, course_id, seats, booked_on)` where `booked_on` is a date string.

Find the `course_id` of every course that was booked four or more times (four or more booking rows) within a single calendar year in each of two consecutive years. In other words, there exists some year Y such that the course has at least 4 bookings in year Y and at least 4 bookings in year Y+1.

Return the result in any order.

Input data

Example rows — the live problem includes the full dataset.

bookings
booking_idcourse_idseatsbooked_on
110012021-01-10
210012021-03-10
310012021-06-10
410012021-09-10
510012022-02-10

Expected output

Your answer should return 1 row with the columns course_id.

Starter code (Pandas (Python))

import pandas as pd

def courses_booked_two_consecutive_years(bookings) -> pd.DataFrame:
    # Your code here
    return bookings

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