Average Workout Length Before and After Joining
Problem
You are given a DataFrame `gym_visits` (loaded from `gym_visits.csv`) with columns `member_id`, `visit_date`, `pass_kind`, and `minutes`. `pass_kind` is one of `'trial'`, `'member'`, or `'lapsed'`, and `minutes` is how long that visit lasted. A member may have many visits.
The gym wants to study members who started on a `trial` pass and later upgraded to a full `member` pass. A member counts as **converted** only if they have at least one `trial` visit and at least one `member` visit.
For each converted member, return their average visit length during the trial phase and during the member phase. Round both averages to two decimals and ignore `lapsed` visits entirely. Return `member_id`, `trial_avg_minutes`, and `member_avg_minutes`, ordered by `member_id`.
Input data
Example rows — the live problem includes the full dataset.
| member_id | visit_date | pass_kind | minutes |
|---|---|---|---|
| 1 | 2024-01-01 | trial | 45 |
| 1 | 2024-01-02 | trial | 30 |
| 1 | 2024-01-05 | trial | 60 |
| 1 | 2024-01-10 | member | 75 |
| 1 | 2024-01-12 | member | 90 |
Expected output
Your answer should return 3 rows with the columns member_id, trial_avg_minutes, member_avg_minutes.
Starter code (Pandas (Python))
import pandas as pd
def converted_member_averages(gym_visits) -> pd.DataFrame:
# Your code here
return gym_visitsSolve 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