0
0
Data Analysis Pythondata~10 mins

Categorical data type optimization in Data Analysis Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to convert the 'color' column to a categorical type.

Data Analysis Python
df['color'] = df['color'].[1]
Drag options to blanks, or click blank then click option'
Aastype('category')
Bstr
Cto_numeric
Dcopy
Attempts:
3 left
💡 Hint
Common Mistakes
Using to_numeric on a string column causes errors.
Using str() does not change the data type in pandas.
Using copy() just duplicates data without changing type.
2fill in blank
medium

Complete the code to check the memory usage of the DataFrame before optimization.

Data Analysis Python
print(df.memory_usage([1]=True))
Drag options to blanks, or click blank then click option'
Amemory
Bverbose
Cindex
Ddeep
Attempts:
3 left
💡 Hint
Common Mistakes
Using verbose does not affect memory usage calculation.
Using index only shows index memory, not full data.
Using memory is not a valid parameter.
3fill in blank
hard

Fix the error in converting the 'city' column to categorical by filling the blank.

Data Analysis Python
df['city'] = df['city'].[1]('category')
Drag options to blanks, or click blank then click option'
Aastype
Bto_category
Cconvert
Dchange_type
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like convert or to_category.
Trying to use change_type which is not a pandas method.
4fill in blank
hard

Fill both blanks to create a dictionary of category counts for the 'fruit' column.

Data Analysis Python
counts = df['fruit'].[1]().[2]()
Drag options to blanks, or click blank then click option'
Avalue_counts
Bcount
Cto_dict
Dunique
Attempts:
3 left
💡 Hint
Common Mistakes
Using count() instead of value_counts() returns total count, not per category.
Using unique() returns unique values, not counts.
5fill in blank
hard

Fill all three blanks to optimize the 'category' column and check its new memory usage.

Data Analysis Python
df['category'] = df['category'].[1]('category')
mem_after = df.memory_usage([2]=[3]).sum()
Drag options to blanks, or click blank then click option'
Aastype
Bdeep
CTrue
Dcopy
Attempts:
3 left
💡 Hint
Common Mistakes
Using copy instead of astype causes no type change.
Not using deep=True underestimates memory usage.