Python
All Top 50 Python & Pandas Interview Questions#18
Optimize DataFrame Memory by Downcasting and Category Dtypes in Pandas
MediumAmazonInterview Question #18
Asked at AmazonA DataFrame consumes 4GB of RAM. Demonstrate how to inspect memory usage with `df.info(memory_usage='deep')`, downcast 64-bit numbers to 32/16-bit, and convert repeated string columns into `category` dtypes.
Input Table: large_df
2 rows preview| user_id | gender | age | rating |
|---|---|---|---|
| 1001 | Male | 28 | 4.5 |
| 1002 | Female | 34 | 4.8 |
Expected Output Structure1 rows
| memory_reduction |
|---|
| 75% RAM reduction |
Interview Context
Asked frequently in data analyst and business analyst technical rounds. Focus on clean filtering, optimal indexing usage, and unambiguous column selection.
Python 3.10 (Pandas)
Environment Ready
Click "Run & Test" to execute your Pandas code against the test assertions.