Report duplicate key groups in a pandas DataFrame as exact duplicates or conflicts
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Write `duplicate_group_report(df, keys)` that emits one row per `keys` group with more than one row (groups of size 1 are excluded). The output columns are the `keys` columns, in order, followed by exactly `['n_rows', 'n_variants', 'kind']`.
The output must meet these requirements:
- `n_rows` (`int64`) is the number of rows in the group.
- `n_variants` (`int64`) is the number of distinct value combinations of the non-key columns in the group. Rows whose non-key values are all `NaN` count as the same variant.
- `kind` (`object`) is `'exact_duplicate'` when `n_variants == 1`, else `'conflicting'`.
- Rows are sorted by `kind` ascending, then `n_rows` descending, then each key column ascending, followed by `reset_index(drop=True)`.
- The function returns only the report DataFrame.
Example:
```python
# keys=['order_id']
# order_id 1001 appears 3 times, all rows identical -> exact_duplicate
# order_id 1002 appears 2 times, amounts differ -> conflicting
# order_id 1003 appears 2 times, non-key cols both NaN -> exact_duplicate
# order_id 1004 appears once -> excluded
order_id n_rows n_variants kind
0 1002 2 2 conflicting
1 1001 3 1 exact_duplicate
2 1003 2 1 exact_duplicate
```
```hint Counting variants
Inside one key group the key columns are constant. Think about which pandas operation counts distinct rows, and how it compares `NaN` with `NaN`.
```
```hint Shape of the output
Check what key value pandas gives you while iterating a groupby on a list of columns, and what dtypes the final frame ends up with.
```
### Constraints & Assumptions
- `keys` is a non-empty list of column names in `df`. Every other column is a non-key column.
- Two `NaN` values in the same non-key column are treated as equal when counting variants.
- The result is compared exactly, so column order, dtypes and the index matter.
### Clarifying Questions to Ask
- Should rows with a missing key value form their own group, or be dropped from the report?
- Should floating-point values that differ only in the last few digits count as a conflict?
- Should strings that differ only in case or surrounding whitespace count as different variants?
### What a Strong Answer Covers
- Grouping by all key columns and excluding groups of size 1.
- Counting variants as distinct non-key combinations, with `NaN` matching `NaN`.
- The exact output layout: key columns in order, then the three report columns with the required dtypes.
- The deterministic sort on `kind`, `n_rows` and the keys, followed by a clean index.
- Edge cases such as missing key values, a frame with no non-key columns, and an input with no duplicated keys.
### Follow-up Questions
- Why should two rows whose non-key columns are all `NaN` count as one variant?
- How would you also show which columns disagree in a conflicting group?
- How would you compute the report without a Python loop over groups on a frame with tens of millions of rows?
Overview: Write a pandas function that finds every key that appears more than once in a DataFrame and reports how many rows and how many distinct non-key variants each key has, labeling the group as an exact duplicate or a conflict. It tests groupby, deduplication that treats missing values as equal, and deterministic sorting.