python - 使用 Pandas 读取大文本文件

标签 python csv pandas ipython large-files

<分区>

我一直在尝试使用 read_csv 函数使用 Pandas 读取一些大文本文件(大小约为 1.4GB - 2GB),但无济于事。以下是我使用的版本:

  • python 2.7.6
  • Anaconda 1.9.2(64 位)(默认,2013 年 11 月 11 日,10:49:15)[MSC v.1500 64 位 (AMD64)]
  • IPython 1.1.0
  • Pandas 0.13.1

我尝试了以下方法:

df = pd.read_csv(data.txt')

它使 Ipython 崩溃并显示一条消息:Kernel died, restarting

然后我尝试使用迭代器:

tp = pd.read_csv('data.txt', iterator = True, chunksize=1000)

我再次遇到Kernel died, restarting 错误。

有什么想法吗?或者任何其他方式来读取大文本文件?

谢谢!

最佳答案

给出了类似问题的解决方案here发布此问题后的一段时间。基本上,它建议通过执行以下操作以 chunks 的形式读取文件:

chunksize = 10 ** 6  # number of rows per chunk
for chunk in pd.read_csv(filename, chunksize=chunksize):
    process(chunk)

您应该根据您机器的能力指定chunksize参数(即确保它可以处理该 block )。

关于python - 使用 Pandas 读取大文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23411619/

相关文章:

mysql - 使用Cron将数据从mysql数据库导出到CSV,然后将所有数据获取到bigquery表

python - 如何让 stat_smooth 在 ggplot-python 中工作?

Python +套接字

python - Python中的Unicode utf-8/utf-16编码

python - 如何使用 python 对正弦波的 1 个周期进行切片?

python - 使用 DictReader 访问 CSV 的第三列时出现 KeyError

python - 共享轴并删除 matplotlib 子图中未使用的

python - 总和忽略 Pandas 数据框中的字符串

python - SQLAlchemy (psycopg2.OperationalError) fe_sendauth : no password supplied

python - request.FILES.getlist ('file' ) 为空