Keep Each Product's Cheapest Offer in First-Appearance Order (pandas)
Quick Overview
Write a pandas function that keeps each product's lowest-price row and returns the products in the order they first appear in the input. It tests sort_values with drop_duplicates, groupby ordering with idxmin, sort stability for ties, and the gap between a row's original position and a group's first appearance.
Keep Each Product's Cheapest Offer in First-Appearance Order (pandas)
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Implement `keep_cheapest_offer_preserve_order(df: pd.DataFrame) -> pd.DataFrame` for a DataFrame `df` with columns `['product', 'price']`, where `price` is a float. Keep only each product's minimum-price row, and order the result by each product's first appearance in the input (not alphabetically and not by price).
The result must:
- Contain exactly the `product` and `price` columns, one row per product.
- Have a fresh 0-based index.
You may assume each product's minimum price is unique (no ties).
For example:
```python
df = pd.DataFrame({
'product': ['B', 'A', 'B', 'A', 'C'],
'price': [5.0, 3.0, 2.0, 4.0, 9.0],
})
keep_cheapest_offer_preserve_order(df)
# product price
# 0 B 2.0
# 1 A 3.0
# 2 C 9.0
```
B comes first because it is the first product in the input.
```hint Two different positions
For each product, keep track of two things separately: the row that holds its cheapest price and the position where the product first shows up. The output order depends on the second one.
```
```hint Check the example
Run your approach on the example and confirm that B comes out first. Ordering the kept rows by their original index labels puts A first, because A's cheapest row sits above B's.
```
### What a Strong Answer Covers
- Selecting the minimum-price row for each product.
- Ordering the output by each product's first appearance, and recognizing that this can differ from the position of the product's cheapest row.
- Returning only `product` and `price` with a fresh 0-based index.
- How ties at the minimum would be resolved, including whether the sort used is stable.
- The time complexity of the chosen approach and edge cases such as missing prices or a non-unique index.
### Follow-up Questions
- How would you handle ties where two rows share the minimum price?
- Could you achieve the same result with `groupby` instead, and what does `groupby` do to the row order by default?
- If the frame also carried a seller and an offer id that must come along with the cheapest row, which of your approaches still work?
Overview: Write a pandas function that keeps each product's lowest-price row and returns the products in the order they first appear in the input. It tests sort_values with drop_duplicates, groupby ordering with idxmin, sort stability for ties, and the gap between a row's original position and a group's first appearance.
Implement keep_cheapest_offer_preserve_order(df: pd.DataFrame) -> pd.DataFrame for a DataFrame df with columns ['product', 'price'], where price is a float. Keep only each product's minimum-price row, and order the result by each product's first appearance in the input (not alphabetically and not by price).
The result must:
Contain exactly the
product
and
price
columns, one row per product.
Have a fresh 0-based index.
You may assume each product's minimum price is unique (no ties).