Aggregate a pandas DataFrame's deep memory usage by column dtype

Quick Overview

Write a pandas function that measures each column's deep memory usage without the index, groups the byte totals by column dtype, and reports the column count, total bytes and share of memory for each dtype. It tests memory profiling, groupby aggregation and deterministic output ordering.

Aggregate a pandas DataFrame's deep memory usage by column dtype

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `memory_profile_by_dtype(df: pd.DataFrame) -> pd.DataFrame`, which aggregates true memory usage by column dtype. Compute per-column bytes with `df.memory_usage(index=False, deep=True)`, then group by dtype. The output must meet these requirements: - The columns are exactly `['dtype', 'n_columns', 'bytes', 'pct_of_total']`, in that order, with one row per distinct dtype. - `dtype` is the dtype as a string (`object` column). `n_columns` is the column count (`int64`). `bytes` is the total deep bytes (`int64`). - `pct_of_total` is `100 * bytes / total deep bytes` of all columns, `float64`, rounded to 2 decimal places. - Exclude the index from every figure. - Sort by `bytes` descending, then `dtype` ascending, then `reset_index(drop=True)`. ```hint Lining things up The per-column byte counts and the column dtypes can both be indexed by column name. Think about which form of the dtype makes a good grouping key, given what the output column has to hold. ``` ### Constraints & Assumptions - The test frame has several `int64` and `float64` columns and two `object` columns holding long strings. - Every figure, including the grand total used for `pct_of_total`, comes from the same index-free deep measurement. ### What a Strong Answer Covers - Measures per-column bytes with `memory_usage(index=False, deep=True)` and explains what `deep` adds for object columns. - Groups by the dtype's string form and reports the column count and summed bytes as `int64`. - Computes `pct_of_total` against the total over all columns, rounded to 2 decimals. - Returns the four columns in order, sorted by `bytes` descending then `dtype` ascending, with a fresh index. ### Follow-up Questions - How would you extend this to also report savings from downcasting numeric columns? - Why can `deep=True` be misleading for some object data, and is it accurate for `category` columns? - What should the function return for a frame that has columns but no rows?

Overview: Write a pandas function that measures each column's deep memory usage without the index, groups the byte totals by column dtype, and reports the column count, total bytes and share of memory for each dtype. It tests memory profiling, groupby aggregation and deterministic output ordering.

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

Write memory_profile_by_dtype(df: pd.DataFrame) -> pd.DataFrame, which aggregates true memory usage by column dtype. Compute per-column bytes with df.memory_usage(index=False, deep=True), then group by dtype.

The output must meet these requirements:

  • The columns are exactly ['dtype', 'n_columns', 'bytes', 'pct_of_total'] , in that order, with one row per distinct dtype.
  • dtype is the dtype as a string ( object column). n_columns is the column count ( int64 ). bytes is the total deep bytes ( int64 ).
  • pct_of_total is 100 * bytes / total deep bytes of all columns, float64 , rounded to 2 decimal places.
  • Exclude the index from every figure.
  • Sort by bytes descending, then dtype ascending, then reset_index(drop=True) .

Constraints & Assumptions

  • The test frame has several int64 and float64 columns and two object columns holding long strings.
  • Every figure, including the grand total used for pct_of_total , comes from the same index-free deep measurement.

What a Strong Answer Covers Guidance

  • Measures per-column bytes with memory_usage(index=False, deep=True) and explains what deep adds for object columns.
  • Groups by the dtype's string form and reports the column count and summed bytes as int64 .
  • Computes pct_of_total against the total over all columns, rounded to 2 decimals.
  • Returns the four columns in order, sorted by bytes descending then dtype ascending, with a fresh index.

Follow-up Questions Guidance

  • How would you extend this to also report savings from downcasting numeric columns?
  • Why can deep=True be misleading for some object data, and is it accurate for category columns?
  • What should the function return for a frame that has columns but no rows?