Flag ID-like columns in a pandas DataFrame by their distinct-value ratio
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Implement `id_like_columns(df, ratio)` to flag identifier-like columns. For each column compute `unique_ratio = nunique(dropna=True) / len(df)` and return a summary DataFrame of the columns with `unique_ratio >= ratio`.
Requirements:
- Output columns are exactly `['column', 'nunique', 'unique_ratio', 'is_unique']`, in that order.
- Dtypes: `column` object, `nunique` int64, `unique_ratio` float64 rounded to 3 decimals, `is_unique` bool.
- The threshold is applied to the rounded `unique_ratio`, the same value that appears in the report.
- `is_unique` is True only when the column is fully distinct with no nulls (its non-null distinct count equals the number of rows).
- Sort by `unique_ratio` descending, then `column` ascending, then `reset_index(drop=True)`.
- If nothing qualifies, or `df` has no rows, return an empty frame with those four columns and dtypes.
Example:
```python
df = pd.DataFrame({
'id': range(1, 11),
'email': ['a@x.com', 'b@x.com', 'c@x.com', 'd@x.com', 'e@x.com',
'f@x.com', 'g@x.com', 'h@x.com', 'i@x.com', None],
'country': ['US', 'US', 'CA', 'US', 'MX', 'CA', 'US', 'US', 'CA', 'US'],
'measure': [1.5, 2.0, 2.0, 3.5, 1.5, 2.0, 4.0, 3.5, 2.0, 1.5],
})
id_like_columns(df, ratio=0.9)
# column nunique unique_ratio is_unique
# 0 id 10 1.0 True
# 1 email 9 0.9 False
```
`email` has 9 distinct non-null values out of 10 rows, so it passes the 0.9 threshold, but its null keeps `is_unique` False. `country` (0.3) and `measure` (0.4) are dropped.
```hint Two different questions
"Looks like a key" and "is a valid key" are separate checks. Work out which count each one needs, and what pandas' built-in uniqueness check does with a single NaN.
```
```hint The empty result
A DataFrame built from no records has no values to infer dtypes from. Build the empty report explicitly.
```
### What a Strong Answer Covers
- Computing the ratio from non-null distinct counts and keeping it separate from the strict no-repeats, no-nulls check
- Applying rounding before the threshold and producing the exact column order, dtypes and deterministic sort
- The zero-row frame and the no-match case, both returning a correctly typed empty report
- Time and space cost, and which kinds of columns this heuristic flags wrongly
### Follow-up Questions
- How would you handle an empty DataFrame (`len(df) == 0`)?
- The 0.9 rule of thumb misfires on small frames. How would you make it more reliable?
- A column of float measurements or event timestamps is almost fully distinct. How would you keep it from being reported as an ID?
Overview: Write a pandas function that flags identifier-like columns by their ratio of distinct values to rows and returns a sorted summary with exact column dtypes. It tests distinct counts with and without nulls, the difference between a cardinality heuristic and a strict uniqueness check, and handling of empty results.