Net Profit per Trading Card
Problem
A collector resells trading cards. Every transaction is logged in one DataFrame.
`card_trades` (`card_trades.csv`): `card_name`, `action`, `trade_day`, `amount`. `action` is either `'purchase'` (money spent acquiring a card) or `'resale'` (money received selling it). `amount` is the money for that single transaction, and `(card_name, trade_day)` is unique. A card may be bought and sold many times.
The **net profit** of a card is the total received from resales minus the total spent on purchases, summed over all its transactions. A negative value means an overall loss.
Return a DataFrame with `card_name` and `net_profit` for each card. Rows may be in any order.
Input data
Example rows — the live problem includes the full dataset.
| card_name | action | trade_day | amount |
|---|---|---|---|
| Solar Drake | purchase | 1 | 1000 |
| Tidecaller | purchase | 2 | 10 |
| Solar Drake | resale | 5 | 9000 |
| Iron Sentinel | purchase | 17 | 30000 |
| Tidecaller | resale | 3 | 1010 |
Expected output
Your answer should return 3 rows with the columns card_name, net_profit.
Starter code (Pandas (Python))
import pandas as pd
def net_profit_per_card(card_trades: pd.DataFrame) -> pd.DataFrame:
# Your code here
return card_tradesSolve 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