Missingness-Aware Frequency Table

Quick Overview

Write a pandas function that builds an SPSS-style frequency table with counts, percent of all rows and percent of non-missing rows, plus a fixed final row for missing values. It tests value_counts, two-key sorting, NaN handling and matching exact DataFrame dtypes.

Missingness-Aware Frequency Table

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement `missing_aware_frequency_table(s: pd.Series) -> pd.DataFrame` returning an SPSS-style frequency table with both percentage conventions. Columns, in order: `level` (object), `count` (int64), `pct_of_all` (float64), `pct_of_valid` (float64). Requirements: - `pct_of_all = count / len(s) * 100`, `pct_of_valid = count / non-null count * 100`. - Observed levels first, sorted by `count` descending, then `level` ascending. - Final row labelled exactly `'(Missing)'` with the NaN count, its `pct_of_all`, and `pct_of_valid` of `np.nan`. - `'(Missing)'` is always present: with no NaNs it has count `0` and `pct_of_all` of `0.0`. - Reset to a fresh `RangeIndex`. Column order, dtypes, and row order must match exactly. Example: ```python s = pd.Series(["a", "a", "b", None, "a", None]) missing_aware_frequency_table(s) ``` ```text level count pct_of_all pct_of_valid 0 a 3 50.000000 75.000000 1 b 1 16.666667 25.000000 2 (Missing) 2 33.333333 NaN ``` Here `len(s)` is 6 and the non-null count is 4, so `a` is 3 / 6 = 50% of all rows and 3 / 4 = 75% of the valid ones. ```hint Tied counts `value_counts` drops missing values by default, but do not rely on the order it gives levels with equal counts. ``` ```hint The missing row The `'(Missing)'` row has a different shape from the observed rows and must exist even when its count is 0. Build it on its own, then check every dtype after you combine the pieces. ``` ### What a Strong Answer Covers - The two denominators: the full length for `pct_of_all` and the non-null count for `pct_of_valid`. - Observed levels sorted by count descending with an ascending-level tie-break. - An unconditional `'(Missing)'` row at the end, with a NaN valid percent and a count of 0 when nothing is missing. - The exact column order, dtypes and fresh `RangeIndex` the frame comparison checks. - What happens when the Series is entirely NaN or empty. ### Follow-up Questions - How would you generalize this to add a cumulative valid-percent column? - What breaks if the input Series is entirely NaN? - When would you show a stakeholder the valid percent instead of the percent of all rows, and when can that choice mislead?

Overview: Write a pandas function that builds an SPSS-style frequency table with counts, percent of all rows and percent of non-missing rows, plus a fixed final row for missing values. It tests value_counts, two-key sorting, NaN handling and matching exact DataFrame dtypes.

PracHub logo
General
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement missing_aware_frequency_table(s: pd.Series) -> pd.DataFrame returning an SPSS-style frequency table with both percentage conventions. Columns, in order: level (object), count (int64), pct_of_all (float64), pct_of_valid (float64).

Requirements:

  • pct_of_all = count / len(s) * 100 , pct_of_valid = count / non-null count * 100 .
  • Observed levels first, sorted by count descending, then level ascending.
  • Final row labelled exactly '(Missing)' with the NaN count, its pct_of_all , and pct_of_valid of np.nan .
  • '(Missing)' is always present: with no NaNs it has count 0 and pct_of_all of 0.0 .
  • Reset to a fresh RangeIndex . Column order, dtypes, and row order must match exactly.

Example:

s = pd.Series(["a", "a", "b", None, "a", None])
missing_aware_frequency_table(s)
       level  count  pct_of_all  pct_of_valid
0          a      3   50.000000     75.000000
1          b      1   16.666667     25.000000
2  (Missing)      2   33.333333           NaN

Here len(s) is 6 and the non-null count is 4, so a is 3 / 6 = 50% of all rows and 3 / 4 = 75% of the valid ones.

What a Strong Answer Covers Guidance

  • The two denominators: the full length for pct_of_all and the non-null count for pct_of_valid .
  • Observed levels sorted by count descending with an ascending-level tie-break.
  • An unconditional '(Missing)' row at the end, with a NaN valid percent and a count of 0 when nothing is missing.
  • The exact column order, dtypes and fresh RangeIndex the frame comparison checks.
  • What happens when the Series is entirely NaN or empty.

Follow-up Questions Guidance

  • How would you generalize this to add a cumulative valid-percent column?
  • What breaks if the input Series is entirely NaN?
  • When would you show a stakeholder the valid percent instead of the percent of all rows, and when can that choice mislead?