search
Search
Join our weekly DS/ML newsletter layers DS/ML Guides
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
chevron_left Meta Information
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
A
A
brightness_medium
share
arrow_backShare
Twitter
Facebook
chevron_left Meta Information
0
thumb_down
0
chat_bubble_outline
0
auto_stories new
settings

Pandas DataFrame | memory_usage method

Programming
chevron_right
Python
chevron_right
Pandas
chevron_right
Documentation
chevron_right
DataFrame
chevron_right
Meta Information
schedule Jul 1, 2022
Last updated
local_offer PythonPandas
Tags

Pandas DataFrame.memory_usage(~) returns the amount of memory each column occupies in bytes.

Parameters

1. index | boolean | optional

Whether to include the memory usage of the index (row labels) as well. By default, index=True.

2. deep | boolean | optional

Whether to look into actual memory usage of object types. For DataFrames that contain object types (e.g. strings), the memory usage would be not be accurate. This is because the method takes a crude estimate on memory consumed by object types. By default, deep=False.

Return Value

A Series that holds the memory usage of each column of the source DataFrame in bytes.

Examples

Basic usage

Consider the following DataFrame:

df = pd.DataFrame({"A":[4,5,6],"B":["K","KK","KKK"], "C": [True,False,True], "D":[4.0,5.0,6.0]}, index=[10,11,12])
df
A B C D
10 4 K True 4.0
11 5 KK False 5.0
12 6 KKK True 6.0

Here's the breakdown of the data-types:

A int64
B object
C bool
D float64
dtype: object

Computing memory usage of each column:

df.memory_usage()
Index 24
A 24
B 24
C 3
D 24
dtype: int64

Columns A and D use types int64 and float64 respectively. 64 bits is equal to 8 bytes, and since we have 3 values in each column, we have a total memory usage of 8*3=24 for columns A and D.

Next, let's tackle the boolean column. A boolean occupies 1 byte each, so again, that column uses 1*3=3 bytes in total.

Finally, let's look at column B, which holds the data-type string. In Pandas, all strings are classified as objects. The method memory_usage, by default, naively assumes that each object takes up 8 bytes of memory without doing any form of inspection. However, the actual memory consumed obviously varies depending on the internals of the object (e.g. a long string occupies more space than a short one). We can get a more accurate representation of the memory usage by setting deep=True.

Specifying deep=True

To get a more accurate representation of the memory consumption of object types:

df.memory_usage(deep=True)
Index 24
A 24
B 185
C 3
D 24
dtype: int64

We see that column B actually takes up 185 bytes.

Specifying index=False

To exclude the memory usage of the index (row labels):

df.memory_usage(index=False)
A 24
B 24
C 3
D 24
dtype: int64
mail
Join our newsletter for updates on new DS/ML comprehensive guides (spam-free)
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
Ask a question or leave a feedback...
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!