AnalystPath

Seven-Day Ridership Trend

PandasMediumMid level~10 min

Problem

You are given a DataFrame `tap` with columns `rider_id`, `rider_name`, `tapped_on`, and `fare_cents`. The pair `(rider_id, tapped_on)` is unique, and each row is one fare a rider paid when tapping into a transit station on a given day. There is at least one tap every day.

You run a transit network and want a smoothed view of revenue. For every day that has **six full days before it** in the data, compute:

- `day_total` — the total fares collected over the seven-day window ending on that day (the day itself plus the six prior days),
- `window_average` — `day_total` divided by 7, rounded to two decimal places.

Return `tapped_on`, `day_total`, and `window_average`, ordered by `tapped_on`. The earliest six days of the dataset never have a full window and are omitted.

**Example**

```text
For seven consecutive days with daily totals 100, 100, 100, 100, 100, 100, 100
the first qualifying day is the seventh, with day_total = 700 and
window_average = 100.00.
```

Input data

Example rows — the live problem includes the full dataset.

tap
rider_idrider_nametapped_onfare_cents
1Mara2023-01-01100
2Theo2023-01-02100
3Lena2023-01-03100
4Otto2023-01-04100
5Iris2023-01-05100

Expected output

Your answer should return 2 rows with the columns tapped_on, day_total, window_average.

Starter code (Pandas (Python))

import pandas as pd

def seven_day_ridership_trend(tap) -> pd.DataFrame:
    # Your code here
    return tap

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