🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.memory_usage()

AI & DATA SCIENCE // df-memory-usage

df.memory_usage() returns a Series showing how many bytes each column of a DataFrame consumes, plus the index itself.

Syntax

df.memory_usage(index=True, deep=False)

Deep Dive Course

By default, memory_usage() reports the shallow memory footprint of each column — for object-dtype columns, like strings, this shallow count only measures the size of the pointers to the actual string objects, not the strings' own memory, which substantially understates the real total memory usage for text-heavy DataFrames. Passing deep=True computes the true memory usage of those object columns by actually inspecting each individual object's size, which is more accurate but noticeably slower to compute.

1Understanding df.memory_usage()

By default, memory_usage() reports the shallow memory footprint of each column — for object-dtype columns, like strings, this shallow count only measures the size of the pointers to the actual string objects, not the strings' own memory, which substantially understates the real total memory usage for text-heavy DataFrames. Passing deep=True computes the true memory usage of those object columns by actually inspecting each individual object's size, which is more accurate but noticeably slower to compute.

💡

Pass deep=True to memory_usage() specifically when a DataFrame has text/object columns and you need an accurate memory estimate — the default shallow calculation significantly understates memory use for those columns, since it only counts pointer sizes, not the actual string data.

editor.html
import pandas as pd

df = pd.DataFrame({"a": [1, 2, 3], "b": [1.0, 2.0, 3.0]})
print(df.memory_usage())
localhost:3000

2Practical Example

Here is a real-world application of df.memory_usage() showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"text": ["short", "a much longer string value"]})
print(df.memory_usage(deep=False))
print(df.memory_usage(deep=True))
localhost:3000

3Best Practices

Follow these guidelines when working with df.memory_usage():

1. Pass deep=True when a DataFrame has object-dtype columns and you need a genuinely accurate memory estimate, not just a quick approximation

2. Sum memory_usage()'s result to get the DataFrame's total memory footprint in one number

3. Use memory_usage() to identify which specific columns are consuming the most memory before deciding where to optimize dtypes, like downcasting int64 to a smaller integer type

⚠️

Tip: Pass deep=True to memory_usage() specifically when a DataFrame has text/object columns and you need an accurate memory estimate — the default shallow calculation significantly understates memory use for those columns, since it only counts pointer sizes, not the actual string data.

editor.html
import pandas as pd

df = pd.DataFrame({"a": [1, 2, 3], "b": [1.0, 2.0, 3.0]})
print(df.memory_usage())
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

df = pd.DataFrame({"a": [1, 2, 3], "b": [1.0, 2.0, 3.0]})
print(df.memory_usage())
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"text": ["short", "a much longer string value"]})
print(df.memory_usage(deep=False))
print(df.memory_usage(deep=True))

Best Practices

  • Pass deep=True when a DataFrame has object-dtype columns and you need a genuinely accurate memory estimate, not just a quick approximation
  • Sum memory_usage()'s result to get the DataFrame's total memory footprint in one number
  • Use memory_usage() to identify which specific columns are consuming the most memory before deciding where to optimize dtypes, like downcasting int64 to a smaller integer type

Interview Question

Why does memory_usage() report a much smaller number for a text column by default, compared to when deep=True is passed?

Hint: Think about what an object-dtype column's array actually stores at each position.

An object-dtype column doesn't store the actual string data directly inside the array itself — it stores a fixed-size pointer, a reference, to each string object, which lives elsewhere in memory. The default, shallow calculation only counts the size of those fixed-size pointers, which is the same regardless of how long the actual strings are, dramatically undercounting the true memory footprint for a column of long strings. Passing deep=True instead follows each pointer and measures the actual size of the string object it points to, adding all of that real memory up for an accurate, if slower to compute, total.

Exercises

MediumPractice using df.memory_usage() in a real scenario.
View Solution
import pandas as pd

df = pd.DataFrame({"a": [1, 2, 3], "b": [1.0, 2.0, 3.0]})
print(df.memory_usage())

Frequently Asked Questions

Why does memory_usage() report a much smaller number for a text column by default, compared to when deep=True is passed?

An object-dtype column doesn't store the actual string data directly inside the array itself — it stores a fixed-size pointer, a reference, to each string object, which lives elsewhere in memory. The default, shallow calculation only counts the size of those fixed-size pointers, which is the same regardless of how long the actual strings are, dramatically undercounting the true memory footprint for a column of long strings. Passing deep=True instead follows each pointer and measures the actual size of the string object it points to, adding all of that real memory up for an accurate, if slower to compute, total.

Related Functions

ndarray-nbytesdf-astypedf-dtypes