AnalystPath

Closest Mileposts on a Trail

PandasEasyJunior level~10 min

Problem

A `Milepost` DataFrame lists the unique integer `position` of each marker along a straight trail. Report the shortest distance between any two markers, in a single column `nearest`.

Input data

Example rows — the live problem includes the full dataset.

milepost
position
5
12
9
30

Expected output

Your answer should return 1 row with the columns nearest.

Starter code (Pandas (Python))

import pandas as pd

def closest_mileposts(milepost: pd.DataFrame) -> pd.DataFrame:
    # Your code here
    return milepost

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