Python
#18

Optimize DataFrame Memory by Downcasting and Category Dtypes in Pandas

MediumAmazon
Interview Question #18
Asked at Amazon

A 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_idgenderagerating
1001Male284.5
1002Female344.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.

Chat with us