Shrink a DataFrame with smaller dtypes
Pandas data engineering interview problem. Difficulty: intermediate. Pattern: Data Quality. About 16 minutes. Part of the Pro drill bank.
Downcast wide numeric and object columns in a DataFrame to minimal safe memory types. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
Given a DataFrame df with columns id (int64), score (float64), and tier (object/string): Optimize the DataFrame's memory footprint by downcasting column data types to the smallest safe representations: Downcast all integer columns to the smallest suitable integer type (pd.to_numeric(..., downcast="integer")). Downcast all floating point columns to the smallest suitable float type (pd.to_numeric(..., downcast="float")). Convert text/object columns to 'category'. Return a summary DataFrame describing the optimized schema with columns: column: column name dtype: string representation of the downcasted column dtype (e.g. 'int8', 'float32', 'category') Sort by column ascending and reset the index. Assign the summary DataFrame to result.
Requirements
- Return column and dtype columns.
- Sort by column ascending.
Constraints
- Values are within valid numeric ranges.
- Object columns contain string categories.
Examples
Input: df (int64, float64, object) id | score | tier 1 | 12.5 | silver 2 | 23.0 | gold 3 | 45.5 | silver 4 | 60.0 | bronze 5 | 99.5 | gold Output: column | dtype id | int8 score | float32 tier | category Integer 1..5 fits in int8, float values downcast to float32, and repetitive string tier becomes category.
Topics: lakebench, pandas, downcast, category.
More interview problems · All interview problems · Learn data engineering
Shrink a DataFrame with smaller dtypes
Interview-style drill: Downcast wide numeric and object columns in a DataFrame to minimal safe memory types.
Given a DataFrame `df` with columns `id` (int64), `score` (float64), and `tier` (object/string): Optimize the DataFrame's memory footprint by downcasting column data types to the smallest safe representations: - Downcast all integer columns to the smallest suitable integer type (`pd.to_numeric(..., downcast="integer")`). - Downcast all floating point columns to the smallest suitable float type (`pd.to_numeric(..., downcast="float")`). - Convert text/object columns to `'category'`. Return a summary DataFrame describing the optimized schema with columns: - `column`: column name - `dtype`: string representation of the downcasted column dtype (e.g. `'int8'`, `'float32'`, `'category'`) Sort by `column` ascending and reset the index. Assign the summary DataFrame to `result`.