Python|熊猫 dataframe.memory_usage()
Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas就是其中之一,它使导入和分析数据变得更加容易。
Pandas dataframe.memory_usage()函数以字节为单位返回每列的内存使用情况。内存使用可以选择包括索引和对象 dtype 元素的贡献。该值默认显示在 DataFrame.info 中。
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
有关代码中使用的 CSV 文件的链接,请单击此处
示例 #1:使用 memory_usage()函数打印数据帧中每一列的内存使用情况以及索引的内存使用情况。
Python3
# importing pandas as pd
import pandas as pd
# Creating the dataframe
df = pd.read_csv("nba.csv")
# Print the dataframe
df
Python3
# Function to find memory use of each
# column along with the index
# even if we do not set index = True,
# it will show the index usage as well by default.
df.memory_usage(index = True)
Python3
# importing pandas as pd
import pandas as pd
# Creating the dataframe
df = pd.read_csv("nba.csv")
# Function to find memory use of each
# column but not of the index
# we set index = False
df.memory_usage(index = False)
让我们使用 memory_usage()函数来查找每一列的内存使用情况。
Python3
# Function to find memory use of each
# column along with the index
# even if we do not set index = True,
# it will show the index usage as well by default.
df.memory_usage(index = True)
输出 :
示例 #2:使用 memory_usage()函数查找每列的内存使用情况,而不是索引的内存使用情况。
Python3
# importing pandas as pd
import pandas as pd
# Creating the dataframe
df = pd.read_csv("nba.csv")
# Function to find memory use of each
# column but not of the index
# we set index = False
df.memory_usage(index = False)
输出 :