Dataframe.memory_usage() in Pandas

In this tutorial, we will learn how much memory is used by a data frame in Pandas using the memory_usage() method. This method displays the memory used by each column of the data frame. The memory displayed is in the form of bytes. This method can be useful when we want to know how much memory is consumed by our data frame. So let’s begin the tutorial.

Arguments in Dataframe.memory_usage()

This method has the following arguments:

index: The default value of this argument is True, which means the memory_usage shows the memory used by the index. It will be the first value displayed for memory_usage(). Users can specify the argument value as False if index memory is not required.

deep: The default value of this argument is False. This is used to introspect the data by taking into consideration the object data types. Users can specify the argument value as False if required.

Dataframe

Here we will create a data frame consisting of the details of 5 people. The details include SNO, Name, Place, Age, Weight and Gender.  We will use the memory_usage() method on this data frame.

import pandas as p
data1 = { 'SNO':[1,2,3,4,5],
          'Name':['a','b','c','d','e'],
          'Place':['Hyderabad','Delhi','Mumbai','Chennai','Kerela'],
          'Age':[20,15,59,90,6],
          'Weight':[57.78,40.0,78.3,90.9834,25.00],
          'Gender':['M','M','F','M','F']
        }
d1 = p.DataFrame(data1) 
print(d1)

OUTPUT:

  SNO Name Place     Age  Weight  Gender
0 1   a    Hyderabad 20   57.7800 M
1 2   b    Delhi     15   40.0000 M
2 3   c    Mumbai    59   78.3000 F
3 4   d    Chennai   90   90.9834 M
4 5   e    Kerela    6    25.0000 F

Using the method without arguments

Here, we will find the memory used without specifying any parameters.

import pandas as p
data1 = { 'SNO':[1,2,3,4,5],
          'Name':['a','b','c','d','e'],
          'Place':['Hyderabad','Delhi','Mumbai','Chennai','Kerela'],
          'Age':[20,15,59,90,6],
          'Weight':[57.78,40.0,78.3,90.9834,25.00],
          'Gender':['M','M','F','M','F']
        }
d1 = p.DataFrame(data1) 
print(d1.memory_usage())

OUTPUT:

Index  128
SNO    40
Name   40
Place  40
Age    40
Weight 40
Gender 40
dtype: int64

Using index argument

We will specify the value as False. By specifying False, the details about the index will not be displayed.

import pandas as p
data1 = { 'SNO':[1,2,3,4,5],
          'Name':['a','b','c','d','e'],
          'Place':['Hyderabad','Delhi','Mumbai','Chennai','Kerela'],
          'Age':[20,15,59,90,6],
          'Weight':[57.78,40.0,78.3,90.9834,25.00],
          'Gender':['M','M','F','M','F']
        }
d1 = p.DataFrame(data1) 
print(d1.memory_usage(index=False))

OUTPUT:

SNO    40
Name   40
Place  40
Age    40
Weight 40
Gender 40
dtype: int64

Using deep argument

We will specify the value as True. By specifying True, the introspection of memory for the object data type is ignored.

import pandas as p
data1 = { 'SNO':[1,2,3,4,5],
          'Name':['a','b','c','d','e'],
          'Place':['Hyderabad','Delhi','Mumbai','Chennai','Kerela'],
          'Age':[20,15,59,90,6],
          'Weight':[57.78,40.0,78.3,90.9834,25.00],
          'Gender':['M','M','F','M','F']
        }
d1 = p.DataFrame(data1) 
print(d1.memory_usage(deep=True))

OUTPUT:

Index  128
SNO    40
Name   310
Place  318
Age    40
Weight 40
Gender 310
dtype: int64

Also read: Dataframe.get() in Pandas with examples

Leave a Reply

Your email address will not be published.