【问题标题】:How to import a gzip file larger than RAM limit into a Pandas DataFrame? "Kill 9" Use HDF5?如何将大于 RAM 限制的 gzip 文件导入 Pandas DataFrame? 《杀戮9》用HDF5?
【发布时间】:2016-11-23 03:59:42
【问题描述】:

我有一个大约 90 GB 的 gzip。这完全在磁盘空间内,但远大于 RAM。

如何将其导入熊猫数据框?我在命令行中尝试了以下操作:

# start with Python 3.4.5
import pandas as pd
filename = 'filename.gzip'   # size 90 GB
df = read_table(filename, compression='gzip')

然而,几分钟后,Python 以Kill 9 关闭。

定义数据库对象df后,我打算将它保存到HDF5中。

这样做的正确方法是什么?我怎样才能使用pandas.read_table() 来做到这一点?

【问题讨论】:

    标签: python pandas dataframe gzip hdf5


    【解决方案1】:

    我会这样做:

    filename = 'filename.gzip'      # size 90 GB
    hdf_fn = 'result.h5'
    hdf_key = 'my_huge_df'
    cols = ['colA','colB','colC','ColZ'] # put here a list of all your columns
    cols_to_index = ['colA','colZ'] # put here the list of YOUR columns, that you want to index
    chunksize = 10**6               # you may want to adjust it ... 
    
    store = pd.HDFStore(hdf_fn)
    
    for chunk in pd.read_table(filename, compression='gzip', header=None, names=cols, chunksize=chunksize):
        # don't index data columns in each iteration - we'll do it later
        store.append(hdf_key, chunk, data_columns=cols_to_index, index=False)
    
    # index data columns in HDFStore
    store.create_table_index(hdf_key, columns=cols_to_index, optlevel=9, kind='full')
    store.close()
    

    【讨论】:

    • 谢谢!您根据脚本是否崩溃(如上)调整chunksize 参数?
    • @JianguoHisiang,是的,您可以做出有根据的猜测...例如,如果您的服务器有 32GB 的 RAM 和 1M (10**6) 行 DF 需要 1GB - 您可以将其增加到 20M (2 * 10**7) 并对其进行测试并检查它是否会给您带来速度优势...
    • 输入文件filename.gzip 没有标题。 cols_to_index 指的是必须已经在数据框中标记的列,对吗?要为无标题的 gzip 文件执行此操作,您是否需要在上面的 pd.read_table() 处为每次迭代中的数据列编制索引?那可能效率低下……
    • @JianguoHisiang,我已经更新了我的答案-请检查
    • @JianguoHisiang,请打开一个新问题,用一个小的可重现样本数据集描述这个问题
    猜你喜欢
    • 2017-02-20
    • 1970-01-01
    • 2012-12-31
    • 2016-01-31
    • 2016-02-22
    • 2021-11-24
    • 2010-11-09
    • 2020-02-02
    相关资源
    最近更新 更多