pandas的内存使用

本文探讨了pandas如何统计内存使用情况,包括`info()`和`memory_usage()`方法。通过调用`info()`,我们可以了解DataFrame的内存占用概况,而`memory_usage()`则提供列级别的详细内存使用数据。对于更准确的内存报告,可以启用`deep=True`选项。数据类型的选取直接影响内存消耗,不同的NumPy dtype会导致不同的内存占用。
摘要由CSDN通过智能技术生成

目录​​​​​​​

统计内存使用情况

info

memory_usage

数据类型和内存的关系


统计内存使用情况

info

ataFram对象调用 info() 时会显示 DataFrame 的内存使用情况(包括索引)。 
例如,调用 info() 时会显示下面的 DataFrame 的内存使用情况:

import pandas as pd
import numpy as np
dtypes = [
        "int8",
        "uint8",
        "int16",
        "int32",
        "int64",
        "float64",
        "datetime64[ns]",
        "timedelta64[ns]",
        "complex128",
        "object",
        "bool",
    ] 
n = 5000

data = {"col_"+t: np.random.randint(100, size=n).astype(t) for t in dtypes}

df = pd.DataFrame(data)

df["categorical"] = df["col_object"].astype("category")

df.info()

# output
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5000 entries, 0 to 4999
Data columns (total 12 columns):
 #   Column               Non-Null Count  Dtype          
---  ------               --------------  -----          
 0   col_int8             5000 non-null   int8           
 1   col_uint8            5000 non-null   uint8          
 2   col_int16            5000 non-null   int16          
 3   col_int32            5000 non-null   int32          
 4   col_int64            5000 non-null   int64          
 5   col_float64          5000 non-null   float64        
 6   col_datetime64[ns]   5000 non-null   datetime64[ns] 
 7   col_timedelta64[ns]  5000 non-null   timedelta64[ns]
 8   col_complex128       5000 non-null   complex128     
 9   col_object           5000 non-null   object         
 10  col_bool             5000 non-null   bool           
 11  categorical          5000 non-null   category       
dtypes: bool(1), category(1), complex128(1), datetime64[ns](1), float64(1), int16(1), int32(1), int64(1), int8(1), object(1), timedelta64[ns](1), uint8(1)
memory usage: 327.2+ KB

+ 符号表示实际内存使用量可能更高,因为 pandas 不计算 dtype=object 列中的值使用的内存。

传递 memory_usage='deep' 将启用更准确的内存使用报告,说明所包含对象的全部使用情况。 这是可选的,因为进行这种更深入的内省可能会很昂贵。

df.info(memory_usage="deep")
# output
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5000 entries, 0 to 4999
Data columns (total 12 columns):
 #   Column               Non-Null Count  Dtype          
---  ------               --------------  -----          
 0   col_int8             5000 non-null   int8           
 1   col_uint8            5000 non-null   uint8          
 2   col_int16            5000 non-null   int16          
 3   col_int32            5000 non-null   int32          
 4   col_int64            5000 non-null   int64          
 5   col_float64          5000 non-null   float64        
 6   col_datetime64[ns]   5000 non-null   datetime64[ns] 
 7   col_timedelta64[ns]  5000 non-null   timedelta64[ns]
 8   col_complex128       5000 non-null   complex128     
 9   col_object           5000 non-null   object         
 10  col_bool             5000 non-null   bool           
 11  categorical          5000 non-null   category       
dtypes: bool(1), category(1), complex128(1), datetime64[ns](1), float64(1), int16(1), int32(1), int64(1), int8(1), object(1), timedelta64[ns](1), uint8(1)
memory usage: 463.8 KB

memory_usage

每列的内存使用情况可以通过调用memory_usage()方法得到。 这将返回一个 Series,其索引由列名和每列的内存使用情况表示,以字节为单位。 对于上面的DataFrame,可以通过memory_usage方法查看每一列的内存使用量和总内存使用量:

如果要获取准确内存时候情况,可以开启参数deep=True

df.memory_usage(deep=True)
# output
Index                     128
col_int8                 5000
col_uint8                5000
col_int16               10000
col_int32               20000
col_int64               40000
col_float64             40000
col_datetime64[ns]      40000
col_timedelta64[ns]     40000
col_complex128          80000
col_object             179800
col_bool                 5000
categorical              9968
dtype: int64


df.memory_usage(deep=True).sum()
#output
474896

数据类型和内存的关系

Data typeDescription
bool_Boolean (True or False) stored as a byte
int_Default integer type (same as C long; normally either int64 or int32)
intcIdentical to C int (normally int32 or int64)
intpInteger used for indexing (same as C ssize_t; normally either int32or int64)
int8Byte (-128 to 127)
int16Integer (-32768 to 32767)
int32Integer (-2147483648 to 2147483647)
int64Integer (-9223372036854775808 to 9223372036854775807)
uint8Unsigned integer (0 to 255)
uint16Unsigned integer (0 to 65535)
uint32Unsigned integer (0 to 4294967295)
uint64Unsigned integer (0 to 18446744073709551615)
float_Shorthand for float64.
float16Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
float32Single precision float: sign bit, 8 bits exponent, 23 bits mantissa
float64Double precision float: sign bit, 11 bits exponent, 52 bits mantissa
complex_Shorthand for complex128.
complex64Complex number, represented by two 32-bit floats
complex128Complex number, represented by two 64-bit floats

Data type objects (dtype) — NumPy v1.22 Manual

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

只要开始永远不晚

谢谢打赏~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值