Build a pandas column profile with dtype, non-null count, missing percentage and distinct count

Quick Overview

Write a pandas function that returns one row per DataFrame column with its dtype, non-null count, missing percentage and distinct count, sorted by missingness. It tests per-column reductions, explicit output dtypes and a deterministic two-key sort.

Build a pandas column profile with dtype, non-null count, missing percentage and distinct count

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write a function `column_profile(df: pd.DataFrame) -> pd.DataFrame` that returns a new DataFrame with one row per column of `df`, summarizing its type, completeness and distinct values. Requirements: - The output columns, in order, are `['column', 'dtype', 'non_null', 'missing_pct', 'nunique']`. - `column` is the column name (`object`). `dtype` is `str(df[c].dtype)`, for example `'int64'`, `'object'` or `'float64'`. - `non_null` is the `int64` non-null count. `missing_pct` is the `float64` value `100 * nulls / len(df)` rounded to 2 decimals. `nunique` is the `int64` count of distinct non-null values (`dropna=True`). - Sort by `missing_pct` descending, then `column` ascending, then `reset_index(drop=True)`. - Do not mutate `df`. ```hint Per-column reductions Each output field is one pandas call on a single column: a count, a ratio or a distinct count. Watch the sort, because the two keys go in opposite directions. ``` ### Constraints & Assumptions - The grader uses a fixed 8-row frame mixing `int64`, `float64` and `object` columns with differing null counts. - `nunique` ignores nulls; `missing_pct` uses the full row count as its denominator. ### What a Strong Answer Covers - Each of the five fields is computed per column with the correct null handling. - The output has the required column order and dtypes. - The sort uses mixed directions on the two keys and ends with a fresh index. - The input frame is left unchanged, and a zero-row frame does not crash. - Time and space complexity. ### Follow-up Questions - How would you add a `top_value` column showing the most frequent value per column? - Why store `str(df[c].dtype)` instead of the dtype object itself? - How would you compute the same profile without a Python loop over the columns?

Overview: Write a pandas function that returns one row per DataFrame column with its dtype, non-null count, missing percentage and distinct count, sorted by missingness. It tests per-column reductions, explicit output dtypes and a deterministic two-key sort.

Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Write a function column_profile(df: pd.DataFrame) -> pd.DataFrame that returns a new DataFrame with one row per column of df, summarizing its type, completeness and distinct values.

Requirements:

  • The output columns, in order, are ['column', 'dtype', 'non_null', 'missing_pct', 'nunique'] .
  • column is the column name ( object ). dtype is str(df[c].dtype) , for example 'int64' , 'object' or 'float64' .
  • non_null is the int64 non-null count. missing_pct is the float64 value 100 * nulls / len(df) rounded to 2 decimals. nunique is the int64 count of distinct non-null values ( dropna=True ).
  • Sort by missing_pct descending, then column ascending, then reset_index(drop=True) .
  • Do not mutate df .

Constraints & Assumptions

  • The grader uses a fixed 8-row frame mixing int64 , float64 and object columns with differing null counts.
  • nunique ignores nulls; missing_pct uses the full row count as its denominator.

What a Strong Answer Covers Guidance

  • Each of the five fields is computed per column with the correct null handling.
  • The output has the required column order and dtypes.
  • The sort uses mixed directions on the two keys and ends with a fresh index.
  • The input frame is left unchanged, and a zero-row frame does not crash.
  • Time and space complexity.

Follow-up Questions Guidance

  • How would you add a top_value column showing the most frequent value per column?
  • Why store str(df[c].dtype) instead of the dtype object itself?
  • How would you compute the same profile without a Python loop over the columns?