Flag Rows Where Quantity Times Price Does Not Match the Total in pandas
Quick Overview
Write a pandas function that adds a boolean column flagging order rows where the stored total does not equal quantity times price, while keeping every row, the column order and the index. It tests vectorized arithmetic checks, copy versus in-place changes, and how missing values and floating-point money affect the comparison.
Flag Rows Where Quantity Times Price Does Not Match the Total in pandas
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `add_total_check(df: pd.DataFrame) -> pd.DataFrame`. The input has columns `['item', 'quantity', 'price', 'total']`, where `item` is a string and the other three are integers. Some rows have an incorrect `total`. Append a boolean column `total_ok` and return the frame.
The function must meet these requirements:
- `total_ok` is `True` exactly where `quantity * price == total`.
- The original columns keep their original order, with `total_ok` as the last column.
- The row count and the original index are preserved.
Example:
```python
df = pd.DataFrame({
'item': ['pen', 'book'],
'quantity': [3, 2],
'price': [2, 5],
'total': [6, 11],
})
add_total_check(df)
# item quantity price total total_ok
# 0 pen 3 2 6 True
# 1 book 2 5 11 False
```
Row 0 is consistent because 3 * 2 = 6. Row 1 is not, because 2 * 5 = 10 and the stored total is 11.
```hint Recompute, then compare
Compute the expected total for every row in one vectorized expression and compare it with the stored column. Decide whether the caller's frame should change.
```
### What a Strong Answer Covers
- How the expected total is computed for all rows and compared with the stored one.
- Why the output keeps every row, the column order and the index, with `total_ok` last.
- Whether the function returns a copy or modifies the input, and the reason for that choice.
- Whether exact equality is the right test for these column types, and what changes when a value is missing or a column is a float.
### Follow-up Questions
- Prices are stored as floats such as 19.99. How does the check change?
- How would you report how far off each incorrect total is?
- Some rows have a missing `quantity` or `price`. What should `total_ok` be for them, and how would you make that behavior explicit?
Overview: Write a pandas function that adds a boolean column flagging order rows where the stored total does not equal quantity times price, while keeping every row, the column order and the index. It tests vectorized arithmetic checks, copy versus in-place changes, and how missing values and floating-point money affect the comparison.
Implement add_total_check(df: pd.DataFrame) -> pd.DataFrame. The input has columns ['item', 'quantity', 'price', 'total'], where item is a string and the other three are integers. Some rows have an incorrect total. Append a boolean column total_ok and return the frame.
The function must meet these requirements:
total_ok
is
True
exactly where
quantity * price == total
.
The original columns keep their original order, with
total_ok
as the last column.
The row count and the original index are preserved.