Complete the code to convert the 'color' column to a categorical type.
df['color'] = df['color'].[1]()
to_numeric() which converts to numbers, not categories.str() which converts to strings, not categories.Using astype('category') converts the column to a categorical type, which saves memory.
Complete the code to check the memory usage of the DataFrame.
memory_before = df.memory_usage([1]=True).sum()
shallow which is not a valid parameter.verbose which is for printing info, not memory calculation.The deep=True option gives a more accurate memory usage including object data.
Fix the error in the code to convert the 'city' column to categorical.
df['city'] = df['city'].[1]('category')
convert() or to_type().The correct method to cast a pandas column type is astype().
Complete the code to calculate the memory usage after the conversions.
memory_after = df.[1]([2]=True).[3]()
info() which shows summary info, not memory.sum() on the Series.Use df.memory_usage(deep=True).sum() to get the total memory usage after converting columns to categorical.
Fill the blanks to compute the percentage of memory saved.
savings_pct = round( ([1] - memory_after) / [1] * [2], [3] )
memory_after instead of memory_before in the denominator.The formula is round( (memory_before - memory_after) / memory_before * 100, 2 ) to get percentage saved rounded to 2 decimals.