What is memory_usage() in pandas?

What is pandas in Python?

pandas is a Python library that provides mechanisms for manipulating and analyzing data.

What are Data frames?

Data frames are the two-dimensional structures that make it easy to inspect data and extract useful information from it.

We must know how much memory each column in the data frame takes up for data processing.

What is the memory_usage() function?

The memory_usage() function is used to get the memory taken up by each column in bytes.

Syntax


dataframe.memory_usage(index, deep)

Arguments

  1. index: Can be set to True or False. The index and its memory usage will be included if it is set to True.

    The default setting is True. (Optional)

  2. deep: Can be set to True or False. A deep calculation of the memory usage will be done if it is set to True, which estimates based on dtypes and the number of rows.

    The default setting is False. (Optional)

Return value

The function returns a pandas series containing the memory usage of each column.

Code

#import library
import pandas as pd
#initialize data
data = {
"p_ID": [1, 2, 3, 4, 5],
"p_name": ['Soap','Brush','Pencil','Notebook','Cream'],
"p_price": [5, 6, 2, 8, 12],
"p_number": [87, 45, 62, 91, 105]
}
#create a data frame
df = pd.DataFrame(data)
#print memory usage with index included
print('Index Included:')
print(df.memory_usage(index = True))
#print memory usage with index excluded
print('\nIndex Excluded:')
print(df.memory_usage(index = False))
#print memory usage with deep calculation
print('\nDeep Calculation:')
print(df.memory_usage(deep = True))

Free Resources