Bird
Raised Fist0
Pandasdata~20 mins

Memory usage analysis in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Memory Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Memory usage of DataFrame columns

What is the output of the following code showing memory usage of each column in bytes?

Pandas
import pandas as pd
import numpy as np
df = pd.DataFrame({
    'A': np.arange(1000, dtype='int64'),
    'B': np.random.rand(1000),
    'C': ['text']*1000
})
mem_usage = df.memory_usage(deep=True)
print(mem_usage)
A
Index    128
A      8000
B      8000
C     24000
dtype: int64
B
Index    128
A      8000
B      8000
C     4000
dtype: int64
C
Index    128
A      8000
B      8000
C     32000
dtype: int64
D
Index    128
A      1000
B      8000
C     24000
dtype: int64
Attempts:
2 left
💡 Hint

Remember that strings with deep=True count the actual string memory, not just pointers.

❓ data_output
intermediate
2:00remaining
Total memory usage of DataFrame

What is the total memory usage in bytes of the DataFrame below, including index and deep memory?

Pandas
import pandas as pd
import numpy as np
df = pd.DataFrame({
    'X': np.random.randint(0, 100, 5000),
    'Y': ['a']*5000
})
total_mem = df.memory_usage(deep=True).sum()
print(total_mem)
A90000
B50000
C100128
D40000
Attempts:
2 left
💡 Hint

Consider index memory plus each column's memory with deep=True.

🔧 Debug
advanced
2:30remaining
Why does memory usage not decrease after dropping a column?

Given the code below, why does the memory usage of df not decrease after dropping column 'B'?

Pandas
import pandas as pd
import numpy as np
df = pd.DataFrame({
    'A': np.arange(10000),
    'B': ['text']*10000
})
print(df.memory_usage(deep=True).sum())
df.drop('B', axis=1, inplace=True)
print(df.memory_usage(deep=True).sum())
ABecause drop with inplace=True does not free memory immediately due to pandas internal caching.
BBecause the 'B' column is still referenced elsewhere in the code, so memory is not freed.
CBecause memory_usage(deep=True) always returns the same value regardless of columns.
DBecause drop does not remove columns, it only hides them temporarily.
Attempts:
2 left
💡 Hint

Think about how pandas manages memory internally and when garbage collection happens.

🧠 Conceptual
advanced
1:30remaining
Effect of categorical dtype on memory usage

Which statement best describes the effect of converting a string column to category dtype on memory usage?

AIt converts strings to integers but increases memory due to overhead.
BIt increases memory usage because categories store extra metadata.
CIt has no effect on memory usage but speeds up computations.
DIt decreases memory usage by storing unique values once and using integer codes.
Attempts:
2 left
💡 Hint

Think about how categories store repeated values efficiently.

🚀 Application
expert
3:00remaining
Optimize memory usage of a large DataFrame

You have a DataFrame with 1 million rows and columns of various types. Which approach will most effectively reduce memory usage without losing data?

AConvert all integer columns to <code>int8</code> and all floats to <code>float16</code> regardless of value range.
BConvert object columns with few unique values to <code>category</code> and downcast numeric columns to smallest suitable subtype.
CDrop all columns with missing values to reduce memory usage.
DConvert all columns to string type to unify data and compress memory.
Attempts:
2 left
💡 Hint

Consider safe downcasting and categorical conversion for repeated strings.

Practice

(1/5)
1. What does the memory_usage() function in pandas primarily show?
easy
A. The amount of memory used by each column and the index in a DataFrame
B. The total number of rows in a DataFrame
C. The data types of each column in a DataFrame
D. The number of missing values in each column

Solution

  1. Step 1: Understand the purpose of memory_usage()

    This function reports memory consumption of each column and the index in a pandas DataFrame.
  2. Step 2: Compare with other options

    Options A, B, and D describe different DataFrame properties unrelated to memory usage.
  3. Final Answer:

    The amount of memory used by each column and the index in a DataFrame -> Option A
  4. Quick Check:

    memory_usage() = memory per column and index [OK]
Hint: memory_usage() shows memory per column and index [OK]
Common Mistakes:
  • Confusing memory_usage() with shape or info()
  • Thinking it shows data types or missing values
  • Ignoring that it includes index memory
2. Which of the following is the correct syntax to get detailed memory usage of a DataFrame df including object types?
easy
A. df.memory_usage()
B. df.memory_usage(True)
C. df.memory_usage(deep=True)
D. df.memory_usage(deep=False)

Solution

  1. Step 1: Recall the parameter for detailed memory info

    The deep=True argument tells pandas to include full memory usage for object columns.
  2. Step 2: Check syntax correctness

    df.memory_usage(deep=True) uses the correct keyword argument deep=True. df.memory_usage(True) uses positional argument incorrectly. Options A, B, and C do not provide detailed memory info.
  3. Final Answer:

    df.memory_usage(deep=True) -> Option C
  4. Quick Check:

    Use deep=True for detailed memory [OK]
Hint: Use deep=True to get full memory info on objects [OK]
Common Mistakes:
  • Passing True without keyword (positional argument error)
  • Not using deep=True and missing object memory
  • Using deep=False which is default and less detailed
3. Given the following code:
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': ['x', 'y', 'z']
})
print(df.memory_usage(deep=True))

What will be the output type of print(df.memory_usage(deep=True))?
medium
A. A pandas Series showing memory usage per column and index
B. A single integer showing total memory usage
C. A dictionary with column names as keys and memory usage as values
D. A DataFrame with memory usage details

Solution

  1. Step 1: Understand the return type of memory_usage()

    The memory_usage() method returns a pandas Series with index as column names plus the index, and values as memory usage in bytes.
  2. Step 2: Check the code output type

    Since deep=True is used, it includes detailed memory for object columns but still returns a Series, not a dict or DataFrame.
  3. Final Answer:

    A pandas Series showing memory usage per column and index -> Option A
  4. Quick Check:

    memory_usage() returns Series [OK]
Hint: memory_usage() returns Series with memory per column [OK]
Common Mistakes:
  • Expecting total memory as single integer
  • Thinking it returns a dictionary or DataFrame
  • Confusing output type with other pandas methods
4. You run df.memory_usage(deep=True) but get an error: TypeError: memory_usage() got an unexpected keyword argument 'deep'. What is the likely cause?
medium
A. You forgot to import pandas as pd
B. You are using an older pandas version that does not support the 'deep' argument
C. You passed 'deep' as a positional argument instead of keyword
D. The DataFrame is empty

Solution

  1. Step 1: Analyze the error message

    The error says 'unexpected keyword argument deep', meaning the function does not recognize 'deep'.
  2. Step 2: Identify cause based on pandas version

    Older pandas versions did not support the 'deep' argument in memory_usage(). This is the most common cause.
  3. Final Answer:

    You are using an older pandas version that does not support the 'deep' argument -> Option B
  4. Quick Check:

    Old pandas versions lack deep argument [OK]
Hint: Check pandas version if 'deep' argument causes error [OK]
Common Mistakes:
  • Assuming import error causes this
  • Thinking positional argument fixes it
  • Blaming empty DataFrame for this error
5. You have a large DataFrame with many object columns. You want to reduce memory usage by converting some columns to categorical type. Which approach helps you analyze memory before and after conversion?
hard
A. Use df.memory_usage() without deep argument before conversion
B. Use df.info() only after conversion
C. Use df.describe() before conversion
D. Use df.memory_usage(deep=True) before and after converting columns to 'category' dtype

Solution

  1. Step 1: Understand memory usage measurement

    Using memory_usage(deep=True) gives detailed memory info including object columns, which is important before conversion.
  2. Step 2: Compare memory before and after conversion

    By running this method before and after converting columns to 'category', you can see the memory savings clearly.
  3. Final Answer:

    Use df.memory_usage(deep=True) before and after converting columns to 'category' dtype -> Option D
  4. Quick Check:

    memory_usage(deep=True) shows detailed memory changes [OK]
Hint: Compare memory_usage(deep=True) before and after changes [OK]
Common Mistakes:
  • Using info() which shows less detailed memory
  • Using describe() which summarizes stats, not memory
  • Using memory_usage() without deep misses object memory
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard