Running account balance

running totals

Running account balance

Stripe Pandas Interview Question

Stripe shows each connected account how its balance changes with every charge, refund and payout. Refunds and payouts are stored as negative amounts.

Using the transactions DataFrame, show every transaction's txn_id, account_id and the account's balance right after it as balance, rounded to 2 decimal places. A balance adds up all of that account's transactions up to and including this one, in date order. Sort the rows by account_id, then by date, earliest first. Assign the answer to result.

Asked of

  • Data Analyst
  • Analytics Engineer
  • Data Engineer
  • Data Scientist
  • ML Engineer

transactionsDataFrame12 rows

Column NameType
txn_idint64
account_idint64
created_datestr
typestr
amountfloat64

transactionsExample Input

txn_idaccount_idcreated_datetypeamount
690012024-08-05payout-150
290022024-08-01charge75.5
1190012024-08-09charge99.99
790032024-08-05charge40
190012024-08-01charge120
1090022024-08-08payout-180
390012024-08-02charge60.25
990032024-08-06charge55.75

Example Output

txn_idaccount_idbalance
19001120
39001180.25
6900130.25
119001130.24
2900275.5
109002-104.5
7900340
9900395.75

Explanation

Account 9001's transactions are not stored in date order. In date order, a charge of 120.00 comes first and then one of 60.25, so the balance reaches 180.25. The payout of 150.00 on August 5 brings it down to 30.25.

The example above is a small slice of the data. Your code runs against the full DataFrames.