pandas
in Python?pandas is a Python library that provides mechanisms for manipulating and analyzing data.
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.
memory_usage()
function?The memory_usage()
function is used to get the memory taken up by each column in bytes.
dataframe.memory_usage(index, deep)
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)
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)
The function returns a pandas
series containing the memory usage of each column.
#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))