Wallet Balances After Transfers
Problem
You are given two DataFrames:
- `wallets` with `wallet_id`, `holder`, `opening` (the starting balance)
- `transfers` with `transfer_id`, `sender_id`, `receiver_id`, `amount`, `sent_on`
Each transfer moves `amount` out of the sender's wallet and into the receiver's wallet. For every wallet compute its final `balance = opening + (total received) - (total sent)`. A wallet that sent or received nothing keeps its opening balance.
Return columns `wallet_id`, `holder`, `balance`, and `overdrawn`, where `overdrawn` is `'Yes'` when the final balance is negative and `'No'` otherwise. Every wallet appears in the output.
Input data
Example rows — the live problem includes the full dataset.
| wallet_id | holder | opening |
|---|---|---|
| 1 | Mireille | 100 |
| 2 | Tariq | 200 |
| 3 | Sven | 10000 |
| 4 | Liwei | 800 |
| transfer_id | sender_id | receiver_id | amount | sent_on |
|---|---|---|---|---|
| 1 | 1 | 3 | 400 | 2021-08-01 |
| 2 | 3 | 2 | 500 | 2021-08-02 |
| 3 | 2 | 1 | 200 | 2021-08-03 |
Expected output
Your answer should return 4 rows with the columns wallet_id, holder, balance, overdrawn.
Starter code (Pandas (Python))
import pandas as pd
def wallet_balances_after_transfers(wallets, transfers) -> pd.DataFrame:
# Your code here
return walletsSolve 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