AnalystPath

Steadily Climbing Athletes

PandasMediumMid level~10 min

Problem

You are given two DataFrames. `athletes` has columns `athlete_id` and `full_name`. `race_results` has columns `result_id`, `athlete_id`, `race_day`, and `fitness_score`; each row is one race result for an athlete.

Find every athlete whose three most recent races (by `race_day`) show a strictly increasing `fitness_score`. Only consider athletes with at least three recorded races. Report the climb amount as the score of the latest of those three races minus the score of the earliest.

Return a DataFrame with columns `athlete_id`, `full_name`, and `gain`, ordered by `gain` descending, then `full_name` ascending.

Input data

Example rows — the live problem includes the full dataset.

athletes
athlete_idfull_name
1Mara Velasquez
2Theo Nakamura
3Priya Anand
4Owen Castellano
5Lena Fischer
race_results
result_idathlete_idrace_dayfitness_score
112024-02-0340
212024-03-0955
312024-04-1260
412024-05-1872
522024-02-0150

Expected output

Your answer should return 2 rows with the columns athlete_id, full_name, gain.

Starter code (Pandas (Python))

import pandas as pd

def steadily_climbing_athletes(athletes, race_results) -> pd.DataFrame:
    # Your code here
    return athletes

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