[P/M/K] simple way to downcast type to reduce memory

本文介绍了一种通过将Pandas DataFrame中的浮点数和整数类型转换为更小的数据类型来减少内存使用的技巧。通过对每列数据类型进行智能下采样,可以显著降低内存消耗,从520.0+bytes降至285.0+bytes。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 11 columns):
Season_Year    5 non-null int64
GameKey        5 non-null int64
PlayID         5 non-null int64
GSISID         5 non-null float64
Time           5 non-null object
x              5 non-null float64
y              5 non-null float64
dis            5 non-null float64
o              5 non-null float64
dir            5 non-null float64
Event          0 non-null float64
dtypes: float64(7), int64(3), object(1)
memory usage: 520.0+ bytes
# Find out the smallest data type possible for each numeric feature
float_cols = df_temp.select_dtypes(include=['float'])
int_cols = df_temp.select_dtypes(include=['int'])

for cols in float_cols.columns:
    df_temp[cols] = pd.to_numeric(df_temp[cols], downcast='float')
    
for cols in int_cols.columns:
    df_temp[cols] = pd.to_numeric(df_temp[cols], downcast='integer')

print(df_temp.info())
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 11 columns):
Season_Year    5 non-null int16
GameKey        5 non-null int8
PlayID         5 non-null int16
GSISID         5 non-null float32
Time           5 non-null object
x              5 non-null float32
y              5 non-null float32
dis            5 non-null float32
o              5 non-null float32
dir            5 non-null float32
Event          0 non-null float32
dtypes: float32(7), int16(2), int8(1), object(1)
memory usage: 285.0+ bytes
None
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值