AnalystPath

Running Wallet Balance

PandasMediumMid level~10 min

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_events
wallet_idevent_datedirectioncents
12024-03-01TopUp5000
12024-03-03Payment1200
12024-03-07Payment800
22024-03-02TopUp3000
22024-03-05TopUp1500

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_events

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