Complete the code to convert the 'color' column to categorical type.
df['color'] = df['color'].[1]()
to_numeric tries to convert to numbers, which is wrong here.str converts to string, not categorical.Using astype('category') converts the column to categorical type, which saves memory and speeds up operations.
Complete the code to check the memory usage of the DataFrame.
memory_before = df.memory_[1](deep=True)
size returns number of elements, not memory.info prints info but does not return memory usage.The memory_usage(deep=True) method returns the memory used by the DataFrame including object types.
Fix the error in the code to convert the 'size' column to categorical.
df['size'] = df['size'].astype([1])
The argument to astype must be the string 'category' to convert to categorical type.
Fill both blanks to create a dictionary of counts for categories with more than 2 entries.
counts = {cat: count for cat, count in df['fruit'].value_counts().items() if count [1] [2]This dictionary comprehension filters categories with counts greater than 2.
Fill all three blanks to create a new categorical column with ordered categories.
df['rating_cat'] = pd.Categorical(df['rating'], categories=[1], ordered=[2]) unique_cats = df['rating_cat'].[3]()
unique without parentheses.We define ordered categories, set ordered=True, and get unique categories with unique().