AnalystPath

Net Profit per Trading Card

PandasMediumMid level~10 min

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_trades
card_nameactiontrade_dayamount
Solar Drakepurchase11000
Tidecallerpurchase210
Solar Drakeresale59000
Iron Sentinelpurchase1730000
Tidecallerresale31010

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_trades

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