使用pandas加载经过过滤的.tda文件的最简单方法是什么? [英] What is the easiest way to load a filtered .tda file using pandas?

查看:88
本文介绍了使用pandas加载经过过滤的.tda文件的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Pandas具有出色的.read_table()功能,但是大文件会导致MemoryError.
由于我只需要加载满足特定条件的线,因此我正在寻找一种仅加载那些条件的线.

Pandas has the excellent .read_table() function, but huge files result in a MemoryError.
Since I only need to load the lines that satisfy a certain condition, I'm looking for a way to only load those.

这可以使用一个临时文件来完成:

This could be done using a temporary file:

with open(hugeTdaFile) as huge:
    with open(hugeTdaFile + ".partial.tmp", "w") as tmp:
        tmp.write(huge.readline())  # the header line
        for line in huge:
            if SomeCondition(line):
                tmp.write(line)

t = pandas.read_table(tmp.name)

有没有办法避免使用临时文件?

Is there a way to avoid such a use of a temp file?

推荐答案

您可以使用chunksize参数返回迭代器

you can use the chunksize parameter to return an iterator

请参见: http ://pandas.pydata.org/pandas-docs/stable/io.html#iterating-through-files-chunk-by-chunk

  • 根据需要过滤块框架
  • 将过滤后的内容追加到列表中
  • 最后的concat

(或者,您也可以将它们写到新的csvs或HDFStores或其他内容中)

(alternatively you could write them out to new csvs or HDFStores or whatever)

这篇关于使用pandas加载经过过滤的.tda文件的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆