栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pandas压缩数据,减少内存占用

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pandas压缩数据,减少内存占用

场景:很多时候,我们需要读取多个文件数据,导致内存增大,甚至报错。

解决方案,在我们读取之后,做一些数据类型的判断,减少内存占用:

import pandas as pd
import numpy as np
from tqdm import tqdm

tqdm.pandas()

# 压缩内存函数,文件大小没变化,占用内存减小
def reduce_mem_usage(df):
    start_memory = df.memory_usage().sum() / 1024 ** 2
    numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
    for col in tqdm(df.columns):
        col_type = df[col].dtypes
        if col_type in numerics:
            c_min = df[col].min()
            c_max = df[col].max()
            if str(col_type)[:3] == 'int':
                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:
                    df[col] = df[col].astype(np.int8)
                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:
                    df[col] = df[col].astype(np.int16)
                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:
                    df[col] = df[col].astype(np.int32)
                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:
                    df[col] = df[col].astype(np.int64)
            else:
                if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:
                    df[col] = df[col].astype(np.float16)
                elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:
                    df[col] = df[col].astype(np.float32)
                else:
                    df[col] = df[col].astype(np.float64)
    end_mem = df.memory_usage().sum() / 1024 ** 2

    print("memory usage after optimization is :", round(end_mem,3))
    print((start_memory - end_mem) / start_memory * 100)

    return df

以上代码的核心思路是将数据类型做匹配。

从一本机器学习竞赛的书中看到的,觉得很不错,在公司的比赛中,还帮到了我。分享给大家~

其中tqdm库是用来显示读取的进度,很方便,有兴趣的可以了解一下。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/321481.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号