Units Dispensed per Drink
Problem
DataFrame: `dispense_log` (`dispense_log.csv`)
```text
+------------+------+
| Column | Type |
+------------+------+
| log_id | int |
| drink_id | int |
| machine_id | int |
| units | int |
+------------+------+
log_id uniquely identifies each row.
Each row records that a vending machine dispensed `units` portions of one drink.
```
Write a function that returns, for every `drink_id`, the total number of `units` dispensed across all log rows. Name the total column `total_units`.
Return the result in any order.
**Example**
Input — `dispense_log`:
```text
log_id drink_id machine_id units
1 100 9 10
2 100 4 12
7 200 4 15
```
Output:
```text
drink_id total_units
100 22
200 15
```
Drink `100` was dispensed in two rows (10 + 12 = 22); drink `200` in one row (15).
Input data
Example rows — the live problem includes the full dataset.
| log_id | drink_id | machine_id | units |
|---|---|---|---|
| 1 | 100 | 9 | 10 |
| 2 | 100 | 4 | 12 |
| 7 | 200 | 4 | 15 |
Expected output
Your answer should return 2 rows with the columns drink_id, total_units.
Starter code (Pandas (Python))
import pandas as pd
def units_per_drink(dispense_log: pd.DataFrame) -> pd.DataFrame:
# Your code here
return dispense_logSolve 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