Build a distribution shape scorecard for the numeric columns of a pandas DataFrame
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Implement `distribution_shape_scorecard(df: pd.DataFrame) -> pd.DataFrame`. It profiles each numeric column of `df` (for example, the columns returned by `select_dtypes(include='number')`) and returns one row per column, ranked worst first.
For each numeric column, compute:
- `mean` and `median`
- `std` with `Series.std()` (`ddof=1`)
- `cv = std / mean`
- `skew` with `Series.skew()`
- `excess_kurtosis` with `Series.kurt()` (a normal distribution gives `0.0`)
Then score the shape of the column:
- `n_flags` counts how many of these three conditions hold (0 to 3): `|skew| >= 1`, `excess_kurtosis >= 1`, `cv >= 1`. The individual boolean flags are not output columns.
- `shape_label` is `'Well-behaved'` for 0 flags, `'Watch'` for 1 flag, and `'Problematic'` for 2 or more.
The output must meet these requirements:
- Columns exactly `['column', 'mean', 'median', 'std', 'cv', 'skew', 'excess_kurtosis', 'n_flags', 'shape_label']`.
- Rows sorted by `n_flags` descending, then `|skew|` descending (a temporary sort key that is dropped afterwards), then `column` ascending, followed by `reset_index(drop=True)`.
- dtypes: `column` and `shape_label` are `object`, `n_flags` is `int64`, and every statistic is `float64`.
```hint Built-in conventions
Check what `Series.std()`, `Series.skew()` and `Series.kurt()` compute by default before you write any moment formulas yourself.
```
```hint Matching the grader
`assert_frame_equal` compares column order, dtypes and the index as well as the values. Think about how to sort on the absolute skew without leaving an extra column behind, and what dtypes an empty result would get.
```
### Constraints & Assumptions
- The grader builds one fixed DataFrame, and every numeric column in it has a positive mean.
- The result is compared with `pandas.testing.assert_frame_equal`, so values, column order, dtypes and the index must all match.
- Non-numeric columns are ignored.
### What a Strong Answer Covers
- Restricting the profile to numeric columns and using the pandas sample conventions for standard deviation, skew and kurtosis.
- Counting the three threshold conditions into `n_flags` and mapping the count to the label, without adding the flags as columns.
- A deterministic order from the three-key sort, with the temporary absolute-skew key dropped.
- The exact column order and dtypes, including for an input with no numeric columns.
- What happens with NaN statistics (very short or all-null columns) and with means close to zero.
### Follow-up Questions
- How would you keep `cv` meaningful when a column mean is near zero or negative?
- Why use excess kurtosis and bias-corrected skew for these thresholds?
- How would you compute the same scorecard for a DataFrame with thousands of numeric columns without a Python loop over columns?
Overview: Write a pandas function that profiles every numeric column of a DataFrame with mean, median, standard deviation, coefficient of variation, skew and excess kurtosis, then counts threshold flags and ranks the columns worst first. It tests pandas sample statistics, deterministic multi-key sorting and exact dtype control for a strict frame comparison.