0
0
Pandasdata~10 mins

Memory savings with categoricals in Pandas - 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.

Pandas
df['color'] = df['color'].[1]()
Drag options to blanks, or click blank then click option'
Aastype('category')
Bto_numeric()
Cstr()
Dcopy()
Attempts:
3 left
💡 Hint
Common Mistakes
Using to_numeric() which converts to numbers, not categories.
Using str() which converts to strings, not categories.
2fill in blank
medium

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

Pandas
memory_before = df.memory_usage([1]=True).sum()
Drag options to blanks, or click blank then click option'
Adetailed
Bshallow
Cdeep
Dverbose
Attempts:
3 left
💡 Hint
Common Mistakes
Using shallow which is not a valid parameter.
Using verbose which is for printing info, not memory calculation.
3fill in blank
hard

Fix the error in the code to convert the 'city' column to categorical.

Pandas
df['city'] = df['city'].[1]('category')
Drag options to blanks, or click blank then click option'
Aconvert
Bastype
Cto_type
Dchange_type
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like convert() or to_type().
4fill in blank
hard

Complete the code to calculate the memory usage after the conversions.

Pandas
memory_after = df.[1]([2]=True).[3]()
Drag options to blanks, or click blank then click option'
Amemory_usage
Bdeep
Csum
Dinfo
Attempts:
3 left
💡 Hint
Common Mistakes
Using info() which shows summary info, not memory.
Forgetting to call sum() on the Series.
5fill in blank
hard

Fill the blanks to compute the percentage of memory saved.

Pandas
savings_pct = round( ([1] - memory_after) / [1] * [2], [3] )
Drag options to blanks, or click blank then click option'
Amemory_before
B100
C2
Dmemory_after
Attempts:
3 left
💡 Hint
Common Mistakes
Using memory_after instead of memory_before in the denominator.
Forgetting to multiply by 100 for percentage.
Wrong number of decimals in round().