AnalystPath

Blocks of Occupied Parking Bays

PandasMediumMid level~10 min

Problem

You are given a DataFrame `occupiedbays` with a single column `bay_no`. Each row is the number of a parking bay that is currently occupied. Bays are numbered consecutively across the lot, and `bay_no` is unique.

Find the contiguous blocks of occupied bays. A block is a run of consecutively numbered bays that are all occupied. For each block, report its first and last bay number.

Return `block_start` and `block_end`, ordered by `block_start`.

Input data

Example rows — the live problem includes the full dataset.

occupiedbays
bay_no
1
2
3
7
8

Expected output

Your answer should return 3 rows with the columns block_start, block_end.

Starter code (Pandas (Python))

import pandas as pd

def occupied_bay_blocks(occupiedbays) -> pd.DataFrame:
    # Your code here
    return occupiedbays

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