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
-
index: Can be set toTrueorFalse. The index and its memory usage will be included if it is set toTrue.The default setting is
True. (Optional) -
deep: Can be set toTrueorFalse. A deep calculation of the memory usage will be done if it is set toTrue, which estimates based ondtypesand 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 libraryimport pandas as pd#initialize datadata = {"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 framedf = pd.DataFrame(data)#print memory usage with index includedprint('Index Included:')print(df.memory_usage(index = True))#print memory usage with index excludedprint('\nIndex Excluded:')print(df.memory_usage(index = False))#print memory usage with deep calculationprint('\nDeep Calculation:')print(df.memory_usage(deep = True))