Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Pandas DataFrame memory_usage() Method

In this tutorial, we will learn the Python pandas DataFrame.memory_usage() method. This method returns the memory usage of each column in bytes that is how many bytes each column holds. After applying this method on the DataFrame, it returns the Series where the index is the column names of the DataFrame and values will be the memory usage of each column in bytes.

The below shows the syntax of the DataFrame.memory_usage() method.

Syntax

The syntax required to use these methods are as follows

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

Parameters

index: It represents the bool(True or False), and the default value is True. It mainly specifies whether to include the memory usage of the DataFrame’s index is returned Series. If index=True, the memory usage of the index is the first item in the output.

deep: It represents the bool(True or False), and the default value is False. If this is True, introspect the data deeply by interrogating object dtypes for system-level memory consumption, and include them in the returned values.

Example: Getting the memory usage using the DataFrame.memory_usage() Method

In the below example, the DataFrame.memory_usage() method returns the memory used by the columns of the DataFrame which includes memory usage by the index of the DataFrame.

#importing pandas as pd
import pandas as pd
#creating the DataFrame
df = pd.DataFrame([['Abhishek',100], ['Anurag',101],['Divya',104]], columns=['Name', 'Roll No'], index=[1,2,3])
print("----------The DataFrame is---------")
print(df)
print("-----------------------------------")
print(df.memory_usage())

Once we run the program we will get the following output.


----------The DataFrame is---------
Name Roll No
1 Abhishek 100
2 Anurag 101
3 Divya 104
-----------------------------------
Index 24
Name 24
Roll No 24
dtype: int64

Example: Set index=False in the DataFrame.memory_usage() Method

In the below example, the DataFrame.memory_usage() method returns the memory used by the column's of the DataFrame which excludes the memory usage by the index of the DataFrame.

#importing pandas as pd
import pandas as pd
#creating the DataFrame
df = pd.DataFrame({'int64':[24,45,78,45],'float64':[1.5,2.5,7.8,4.5]})
print("----------The DataFrame is---------")
print(df)
print("-----------------------------------")
print(df.memory_usage(index=False))

Once we run the program we will get the following output.


----------The DataFrame is---------
int64 float64
0 24 1.5
1 45 2.5
2 78 7.8
3 45 4.5
-----------------------------------
int64 32
float64 32
dtype: int64

Example: Get overall memory consumption by DataFrame

We can get the overall memory consumption of the DataFrame column's using the DataFrame.memory_usage() method along with the sum() function. The below example shows the same.

#importing pandas as pd
import pandas as pd
#creating the DataFrame
df = pd.DataFrame({'int64':[24,45,78],'float64':[2.5,7.8,4.5]})
print("----------The DataFrame is---------")
print(df)
print("------------------------------------")
print(df.memory_usage(index=False))
print("-----------------------------------")
print("The total memory used by the DataFrame column's is:",df.memory_usage(index=False).sum())

Once we run the program we will get the following output.



----------The DataFrame is---------
int64 float64
0 24 2.5
1 45 7.8
2 78 4.5
------------------------------------
int64 24
float64 24
dtype: int64
-----------------------------------
The total memory used by the DataFrame column's is: 48

Conclusion

In this tutorial, we learned the python pandas DataFrame.memory_usage() method. We understand the syntax and get the memory usage information by solving examples.