Summarize a pandas DataFrame's columns by dtype with counts and percentages

Quick Overview

Write a pandas function that summarizes a DataFrame's schema with one row per distinct dtype, the number of columns of that dtype and their share of all columns. It tests working with df.dtypes, exact output dtypes, rounding and a deterministic tie-breaking sort.

Summarize a pandas DataFrame's columns by dtype with counts and percentages

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `def dtype_census(df: pd.DataFrame) -> pd.DataFrame` that returns a new DataFrame with one row per distinct dtype in `df` and exactly the columns `['dtype', 'n_columns', 'pct_of_columns']`, in that order. The output must meet these requirements: - `dtype` is the dtype as a string, for example `'int64'`, stored in an `object` column. - `n_columns` is the `int64` count of columns with that dtype. - `pct_of_columns` is a `float64` equal to `100 * n_columns / total_columns`, rounded to 1 decimal. - Rows are sorted by `n_columns` descending, then `dtype` ascending, followed by `reset_index(drop=True)`. - The input DataFrame is not mutated. Example: ```python # df has 3 int64, 2 object, 2 float64 and 1 bool column (8 total) dtype_census(df) # dtype n_columns pct_of_columns # 0 int64 3 37.5 # 1 float64 2 25.0 # tie on n_columns: float64 sorts before object # 2 object 2 25.0 # 3 bool 1 12.5 ``` ```hint Where the dtypes live `df.dtypes` already holds one entry per column. Turn it into something you can count, and remember that a tie on the count still needs a deterministic order. ``` ### What a Strong Answer Covers - Reading one dtype per column from `df.dtypes` and counting by its string form. - The exact output columns, their order, and their dtypes (`object`, `int64`, `float64`). - A percentage computed against the total number of columns and rounded to one decimal. - The two-key sort that makes ties deterministic, followed by a clean index. - Leaving the input untouched, and the behavior for a DataFrame with no columns. ### Follow-up Questions - How would you extend this to also report memory usage per dtype? - What should the function return for a DataFrame with zero columns? - Two categorical columns have different categories. Should they count as one dtype or two, and how would you change the grouping if you wanted two?

Overview: Write a pandas function that summarizes a DataFrame's schema with one row per distinct dtype, the number of columns of that dtype and their share of all columns. It tests working with df.dtypes, exact output dtypes, rounding and a deterministic tie-breaking sort.

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

Write def dtype_census(df: pd.DataFrame) -> pd.DataFrame that returns a new DataFrame with one row per distinct dtype in df and exactly the columns ['dtype', 'n_columns', 'pct_of_columns'], in that order.

The output must meet these requirements:

  • dtype is the dtype as a string, for example 'int64' , stored in an object column.
  • n_columns is the int64 count of columns with that dtype.
  • pct_of_columns is a float64 equal to 100 * n_columns / total_columns , rounded to 1 decimal.
  • Rows are sorted by n_columns descending, then dtype ascending, followed by reset_index(drop=True) .
  • The input DataFrame is not mutated.

Example:

# df has 3 int64, 2 object, 2 float64 and 1 bool column (8 total)
dtype_census(df)
#      dtype  n_columns  pct_of_columns
# 0    int64          3            37.5
# 1  float64          2            25.0   # tie on n_columns: float64 sorts before object
# 2   object          2            25.0
# 3     bool          1            12.5

What a Strong Answer Covers Guidance

  • Reading one dtype per column from df.dtypes and counting by its string form.
  • The exact output columns, their order, and their dtypes ( object , int64 , float64 ).
  • A percentage computed against the total number of columns and rounded to one decimal.
  • The two-key sort that makes ties deterministic, followed by a clean index.
  • Leaving the input untouched, and the behavior for a DataFrame with no columns.

Follow-up Questions Guidance

  • How would you extend this to also report memory usage per dtype?
  • What should the function return for a DataFrame with zero columns?
  • Two categorical columns have different categories. Should they count as one dtype or two, and how would you change the grouping if you wanted two?