AnalystPath

Running Total of Donations per Donor

PandasMediumMid level~10 min

Problem

A charity records each donation a supporter makes on a given date.

DataFrame: `gifts` (from `gifts.csv`)

| Column | Type |
|-------------|--------|
| donor_id | int |
| campaign_id | int |
| gift_date | object |
| amount | int |

Write a function that reports, for each donor and each of their gift dates, the **cumulative total** the donor has given up to and including that date. Return columns `donor_id`, `gift_date`, and `total_so_far`, in any order.

Input data

Example rows — the live problem includes the full dataset.

gifts
donor_idcampaign_idgift_dateamount
122019-03-015
122019-05-026
132020-06-251
312019-03-020
342021-07-035

Expected output

Your answer should return 5 rows with the columns donor_id, gift_date, total_so_far.

Starter code (Pandas (Python))

import pandas as pd

def running_total(gifts: pd.DataFrame) -> pd.DataFrame:
    # Your code here
    return gifts

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