Python | Pandas Series.memory_usage ()

Python Methods and Functions

Series.memory_usage() Pandas Series.memory_usage() returns the memory usage of Series. Memory usage can optionally include the contribution of the index and dtype elements.

Syntax: Series.memory_usage (index = True, deep = False)

Parameter:
index: Specifies whether to include the memory usage of the Series index.
deep: If True, introspect the data deeply by interrogating object dtypes for system-level memory consumption, and include it in the returned value.

Returns: Bytes of memory consumed.

Example # 1: Use the Series.memory_usage () function to find the memory usage of a given series of an object.

# import pandas as pd

import pandas as pd

 
< code class = "comments"> # Create series

sr = pd.Series ([ 10 , 25 , 3 , 25 , 24 , 6 ])

  
# Create index

index_ = [ `Coca Cola` , ` Sprite` , `Coke` , ` Fanta` , `Dew` , `ThumbsUp` ]

 
# set index

sr.index = index_

 
# Print series

print (sr)

Output:

We will now use Series.memory_usage () to find the memory usage for a given series object.

# revert memory usage

result =   sr.memory_usage ()

 
# Print result

print (result)

Output:


As we can see from the output, Series.memory_usage () successfully returned the memory usage of this series object.

Example # 2: Use Series.memory_usage () to find the memory usage for a given series object.

# import pandas as pd

import pandas as pd

 
# Create a series

< p> sr = pd.Series ([ 19.5 , 16.8 , None , 22.78 , 16.8 , 20.124 , None , 18.1002 , 19.5 ])

 
# Print series

print (sr)

Output:

Now we will We use Series.memory_usage () to find the memory usage for a given series object.

# revert memory usage

result = sr.memory_usage ()

 
# Print result

print (result)

Output:


Like us we can see from the output, Series.memory_usage () successfully returned the memory usage of the given series object.





Get Solution for free from DataCamp guru