Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pandas
communitycreator
memory_usage()
python

What is memory_usage() in pandas?

Eman Kashif

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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))

RELATED TAGS

pandas
communitycreator
memory_usage()
python

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring