Python | Pandas dataframe.memory_usage ()

Python Methods and Functions

The Pandas function dataframe.memory_usage() returns the memory usage of each column in bytes. Memory usage can optionally include the contribution of the index and the elements of the dtype object. This value is displayed in DataFrame.info by default.

Syntax: DataFrame.memory_usage (index = True, deep = False)

Parameters:
index: Specifies whether to include the memory usage of the DataFrame`s index in returned Series. If index = True the memory usage of the index the first item in the output.
deep: If True, introspect the data deeply by interrogating object dtypes for system-level memory consumption, and include it in the returned values.

Returns: A Series whose index is the original column names and whose values ​​is the memory usage of each column in bytes

To link to the CSV file used in the code, click here

Example # 1: Using the memory_usage () function prints memory usage information for each column in the data frame, along with index usage.

# import pandas as pd

import pandas as pd

 
# Create a data frame

df = pd.read_csv ( " nba.csv " )

  
# Print the data frame
df

Let`s use memory_usage () to find the memory usage for each column.

# Function to find memory usage of each
# column with index
# even if we don`t set index = True,
# it will show using the default index.

df.memory_usage (index = True )

Output:

Example # 2: Use memory_usage () to find the memory usage of each column, but not index.

# import pandas as pd

import pandas as pd

 
# Create a data frame

df = pd.read_csv ( "nba .csv "  )

 
# Function to find memory usage of each
# column but not index
# we set index = False

df.memory_usage (index = False )

Output:





Get Solution for free from DataCamp guru