Python Pandas - Return the memory usage of the Index values

The index.memory_usage() method in Pandas returns the memory consumption of an Index in bytes. This is useful for monitoring memory efficiency and optimizing performance in data analysis.

Syntax

Index.memory_usage(deep=False)

Parameters

deep (bool, default False): If True, introspect the data deeply and calculate the memory usage of object dtypes more accurately.

Basic Memory Usage

Here's how to check the memory usage of a simple Index ?

import pandas as pd

# Creating a numeric index
index = pd.Index([15, 25, 35, 45, 55])

print("Pandas Index...")
print(index)
print("\nMemory usage:", index.memory_usage(), "bytes")
Pandas Index...
Index([15, 25, 35, 45, 55], dtype='int64')

Memory usage: 40 bytes

Memory Usage with Different Data Types

Different data types consume different amounts of memory ?

import pandas as pd

# Integer Index
int_index = pd.Index([1, 2, 3, 4, 5])
print("Integer Index memory usage:", int_index.memory_usage(), "bytes")

# String Index  
str_index = pd.Index(['apple', 'banana', 'cherry', 'date', 'elderberry'])
print("String Index memory usage:", str_index.memory_usage(), "bytes")
print("String Index memory usage (deep):", str_index.memory_usage(deep=True), "bytes")
Integer Index memory usage: 40 bytes
String Index memory usage: 40 bytes
String Index memory usage (deep): 274 bytes

Comparing Index Properties

Understanding the relationship between memory usage and other Index properties ?

import pandas as pd

index = pd.Index([10, 20, 30, 40, 50, 60])

print("Index:", index.tolist())
print("Size (number of elements):", index.size)
print("Shape:", index.shape)
print("Data type:", index.dtype)
print("Bytes in data:", index.nbytes)
print("Dimensions:", index.ndim)
print("Memory usage:", index.memory_usage(), "bytes")
Index: [10, 20, 30, 40, 50, 60]
Size (number of elements): 6
Shape: (6,)
Data type: int64
Bytes in data: 48
Dimensions: 1
Memory usage: 48 bytes

Comparison Table

Parameter Description Use Case
deep=False Shallow memory calculation Quick memory estimation
deep=True Accurate memory calculation for objects Precise memory analysis for strings/objects

Conclusion

The memory_usage()deep=True for accurate memory calculation of object data types like strings.

Updated on: 2026-03-26T16:15:24+05:30

237 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements