AnalystPath

Swap Adjacent Relay Lanes

PandasMediumMid level~10 min

Problem

You are given a DataFrame `lane` with columns `lane_no` and `runner`. The `lane_no` values form a continuous sequence starting at 1.

Swap the runners in every pair of consecutive lanes: lanes 1 and 2 trade runners, lanes 3 and 4 trade runners, and so on. If the highest lane number is odd, that last lane keeps its runner. Return the columns `lane_no` and `runner`, ordered by `lane_no`.

Input data

Example rows — the live problem includes the full dataset.

lane
lane_norunner
1Reyes
2Okafor
3Petrov
4Nakamura
5Costa

Expected output

Your answer should return 5 rows with the columns lane_no, runner.

Starter code (Pandas (Python))

import pandas as pd

def swap_adjacent_relay_lanes(lane) -> pd.DataFrame:
    # Your code here
    return lane

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