如何避免将多个文件加载到内存中?

2024-09-24 00:36:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我将不同的CSV文件作为数据帧加载到内存中,这可能会导致OutOfMemory错误。我一次把它们都装进去,因为我要做连接操作

有没有办法避免一次将所有文件加载到内存中

myfiles= load_all_files(mypath)

myfiles.df1 = myfiles.df1\
    .query("IdType not in ['A','B','C']")\
    .drop(columns='IdType')\
    .groupby('IdGroup')\
    .agg('sum')\
    .join(myfiles.df2)\
    .groupby('IdV')\
    .agg('sum')

myfiles.d3 = myfiles.df3\
    .query("IdType not in [1,2,3]")\
    .drop(columns='IdType')\
    .groupby('IdGroup')\
    .agg('sum')\
    .join(myfiles.df4)\
    .groupby('IdV')\
    .agg('sum')

# other similar operations

Tags: columns文件内存innotqueryaggdrop