pandas.DataFrame.memory_usage#
- DataFrame.memory_usage(index=True, deep=False)[源代码]#
返回每列的内存使用量(以字节为单位)。
内存使用量还可以包括索引和 object dtype 元素的贡献。
此值默认在 DataFrame.info 中显示。可以通过将 ``pandas.options.display.memory_usage``设置为 False 来抑制此行为。
- Parameters:
- indexbool, default True
指定是否将在返回的 Series 中包含 DataFrame 索引的内存使用量。如果
index=True,索引的内存使用量是输出中的第一项。- deepbool,默认 False
如果为 True,则通过查询 object dtypes 的系统级内存消耗来深入检查数据,并将其包含在返回值中。
- Returns:
- Series
一个 Series,其索引是原始列名,其值是以字节为单位的每列内存使用量。
参见
numpy.ndarray.nbytesndarray 元素消耗的总字节数。
Series.memory_usageSeries 消耗的字节数。
Categorical用于字符串值(具有许多重复值)的内存高效数组。
DataFrame.infoDataFrame 的简明摘要。
Notes
有关更多详细信息,请参阅 Frequently Asked Questions 。
Examples
>>> dtypes = ['int64', 'float64', 'complex128', 'object', 'bool'] >>> data = dict([(t, np.ones(shape=5000, dtype=int).astype(t)) ... for t in dtypes]) >>> df = pd.DataFrame(data) >>> df.head() int64 float64 complex128 object bool 0 1 1.0 1.0+0.0j 1 True 1 1 1.0 1.0+0.0j 1 True 2 1 1.0 1.0+0.0j 1 True 3 1 1.0 1.0+0.0j 1 True 4 1 1.0 1.0+0.0j 1 True
>>> df.memory_usage() Index 128 int64 40000 float64 40000 complex128 80000 object 40000 bool 5000 dtype: int64
>>> df.memory_usage(index=False) int64 40000 float64 40000 complex128 80000 object 40000 bool 5000 dtype: int64
默认情况下,object dtype 列的内存占用将被忽略:
>>> df.memory_usage(deep=True) Index 128 int64 40000 float64 40000 complex128 80000 object 180000 bool 5000 dtype: int64
对于具有许多重复值的 object-dtype 列,请使用 Categorical 来实现高效存储。
>>> df['object'].astype('category').memory_usage(deep=True) 5244