You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pandas to_csv追加模式是否读取目标文件至内存?大数据分块OOM求助

Pandas to_csv追加模式的内存问题解析

问题描述

分块处理大数据集时遭遇内存不足(out-of-memory)错误,观察内存图表有11次峰值与谷值,程序在第11次迭代时崩溃。推测使用to_csv追加模式时,Pandas会先将目标文件读入内存,在内存中完成追加后再整体写入磁盘,想确认这一机制是否存在。

用户代码实现:

dfd,dump=tempfile.mkstemp(".csv")
n_chunks=getNumChunks()
print(f"Working in {n_chunks} iteration(s).")
for current_chunk in range(1,n_chunks+1):
    print(f"Iteration {current_chunk} of {n_chunks} ({current_chunk/n_chunks*100:.2f}%)")
    full=mydb.query(f"""
          select colA,colB
          from mytable
          where chunk={current_chunk}
    """)
            
    if full.empty:
        continue
    
    #Get a dataset with the required input cols.
    score_me = full[input_cols].copy()
    print(f"Scoring {len(full)} records.")
    full['value']=clf.predict(score_me)
    
    new=full[['colA','colB','other','cols','tokeep']]
    total_scored+=new.shape[0]
    
    #This is where I append:
    new.to_csv(dump,mode="a+",header=False,index=False)
    new=full=score_me=None
#End of chunks loop
print(f"Finished scoring {total_scored:,.0f} records.")
os.close(dfd)
os.remove(dump)

核心结论

Pandas的to_csv在追加模式(mode="a+")下不会读取目标文件到内存。它的逻辑是直接打开文件并在末尾写入当前DataFrame的内容,不会加载已有文件数据。

内存峰值的原因分析

内存峰值与迭代次数对应,问题大概率出在:

  • 每次迭代中生成的full、score_me、new等DataFrame对象,虽然被赋值为None,但Python垃圾回收可能未及时释放内存,导致内存逐步累积。
  • 模型clf在预测过程中可能占用额外内存,单块数据量较大时,多次迭代后内存无法有效回收。

优化建议

  • 显式触发垃圾回收:在每次迭代末尾添加gc.collect(),强制回收未被引用的内存:
    import gc
    # 迭代内处理逻辑结束后
    new=full=score_me=None
    gc.collect()
    
  • 复用文件句柄:避免每次调用to_csv都打开/关闭文件,提前打开文件句柄循环复用:
    dfd,dump=tempfile.mkstemp(".csv")
    with open(dump, 'a', newline='') as f:
        for current_chunk in range(1,n_chunks+1):
            # 处理逻辑...
            new.to_csv(f, header=False, index=False)
            # 内存清理...
    
  • 减小单块数据量:检查getNumChunks()的块数设置,尝试拆分出更小的数据块,降低单次迭代的内存占用。
  • 避免不必要复制:若无需修改原数据,score_me = full[input_cols].copy()可改为score_me = full[input_cols],减少内存复制开销。

内容的提问来源于stack exchange,提问作者Chris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 05:37:12