Running Wallet Balance
Problem
You are given a DataFrame `wallet_events` that logs every money movement for each user. Each row has a `wallet_id`, an `event_date` (a `YYYY-MM-DD` string), a `direction` that is either `'TopUp'` (money added) or `'Payment'` (money spent), and the `cents` involved (always a positive integer).
For each event, report the wallet's running balance up to and including that event. The balance starts at 0; a `'TopUp'` adds `cents` and a `'Payment'` subtracts `cents`. Process each wallet's events in date order. Return `wallet_id`, `event_date`, and the resulting `running_cents`, ordered by `wallet_id` then `event_date`.
Input data
Example rows — the live problem includes the full dataset.
| wallet_id | event_date | direction | cents |
|---|---|---|---|
| 1 | 2024-03-01 | TopUp | 5000 |
| 1 | 2024-03-03 | Payment | 1200 |
| 1 | 2024-03-07 | Payment | 800 |
| 2 | 2024-03-02 | TopUp | 3000 |
| 2 | 2024-03-05 | TopUp | 1500 |
Expected output
Your answer should return 5 rows with the columns wallet_id, event_date, running_cents.
Starter code (Pandas (Python))
import pandas as pd
def running_wallet_balance(wallet_events) -> pd.DataFrame:
# Your code here
return wallet_eventsSolve 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