Mean-Median Gap and Pearson Skewness Report for Every Numeric Column
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Write `mean_median_gap_report(df: pd.DataFrame) -> pd.DataFrame`. For every numeric column, compute the mean-median gap and Pearson's second skewness coefficient, then classify direction. Return one row per column.
Requirements:
- `gap = mean - median`, `pearson_skew2 = 3 * (mean - median) / std` (`Series.std()`, `ddof=1`).
- `direction`: `pearson_skew2 > 0.2` gives `'Right-skewed'`, `< -0.2` gives `'Left-skewed'`, otherwise `'Symmetric'` (exactly 0.2 or -0.2 is `'Symmetric'`).
- Columns exactly `['column', 'mean', 'median', 'gap', 'pearson_skew2', 'direction']`.
- Sort by `pearson_skew2` descending, then `column` ascending, then `reset_index(drop=True)`.
- dtypes: `column` and `direction` are `object`, the others `float64`.
- The result is compared with `assert_frame_equal` against a fixed DataFrame that has a right-tailed, a left-tailed and a symmetric column. All columns are numeric with non-zero variance.
Example:
```python
df = pd.DataFrame({
"left": [-10, -4, -3, -2, -1],
"right": [1, 2, 3, 4, 10],
"sym": [1, 2, 3, 4, 5],
})
mean_median_gap_report(df)
```
```text
column mean median gap pearson_skew2 direction
0 right 4.0 3.0 1.0 0.848528 Right-skewed
1 sym 3.0 3.0 0.0 0.000000 Symmetric
2 left -4.0 -3.0 -1.0 -0.848528 Left-skewed
```
For `right`, the mean is 20 / 5 = 4 and the median is 3. The squared deviations from 4 sum to 50, so the `ddof=1` std is the square root of 12.5, about 3.536, and 3 * 1 / 3.536 is about 0.849.
```hint Order of operations
Build one record per column first, then sort the finished frame on both keys. Check which dtypes pandas gives each column before you compare.
```
### What a Strong Answer Covers
- Correct per-column statistics, including the `ddof=1` std and the factor of 3.
- The direction rule, with values of exactly 0.2 or -0.2 labeled `'Symmetric'`.
- The exact column order, the two-key sort, the reset index and the required dtypes.
- How NaN values and a constant column would affect the result.
### Follow-up Questions
- How does this differ from Pearson's first skewness coefficient?
- How would you handle a column with zero standard deviation?
- Can the sign of the mean-median gap disagree with the moment-based skewness that `Series.skew()` returns?
Overview: Write a pandas function that reports, for every numeric column, the mean, the median, their gap and Pearson's second skewness coefficient, then labels each column right-skewed, left-skewed or symmetric. It tests per-column aggregation, threshold rules, sorting on two keys and exact output dtypes.