Python | Pandas Index.memory_usage()

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

Pandas Index.memory_usage() function return the memory usage of the Index. It returns the sum of the memory used by all the individual labels present in the Index.

Syntax: Index.memory_usage(deep=False)

Parameters :
deep : Introspect the data deeply, interrogate object dtypes for system-level memory consumption

Returns : bytes used

Example #1: Use Index.memory_usage() function to find the overall memory used by the Index object.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas as pd
import pandas as pd
  
# Creating the Index
idx = pd.Index(['Labrador', 'Beagle', 'Mastiff', 'Lhasa', 'Husky', 'Beagle'])
  
# Print the Index
idx

chevron_right


Output :

Now we will use Index.memory_usage() function to find the memory usage of the idx object.

filter_none

edit
close

play_arrow

link
brightness_4
code

# finding the memory used by the idx object
idx.memory_usage()

chevron_right


Output :

The function has returned the value of 48 indicating that 48 bytes of memory are being used.
 
Example #2: Use Index.memory_usage() function to check the memory usage of the MultiIndex object.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas as pd
import pandas as pd
  
# Creating the MutiIndex
midx = pd.MultiIndex.from_arrays([['Mon', 'Tue', 'Wed', 'Thr'], [10, 20, 30, 40]],
                                                       names =('Days', 'Target'))
  
# Print the MultiIndex
midx

chevron_right


Output :

Now we will check the amount of memory used by the midx object.

filter_none

edit
close

play_arrow

link
brightness_4
code

# return the total memory used by the multi-index object
midx.memory_usage()

chevron_right


Output :

As we can see in the output, the function has returned 180 indicating that the midx object is using 180 bytes of memory.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.