python - Pandas 和 HDF5 中的文件大小减小

标签 python numpy pandas hdf5 pytables

我正在运行一个模型,该模型将数据输出到多个 Pandas 帧中,然后将这些帧保存到 HDF5 文件中。该模型运行数百次,每次都会将新列(多索引)添加到现有 HDF5 文件的帧中。这是通过 Pandas merge 完成的。由于每次运行的帧长度不同,因此帧中最终会出现大量 NaN 值。

完成足够的模型运行后,如果行或列与出现错误的模型运行关联,则数据将从帧中删除。在此过程中,新的数据帧被放入新的 HDF5 文件中。下面的伪Python展示了这个过程:

with pandas.HDFStore(filename) as store:
    # figure out which indices should be removed
    indices_to_drop = get_bad_indices(store)

    new_store = pandas.HDFStore(reduced_filename) 
    for key in store.keys():
        df = store[key]
        for idx in indices_to_drop:
             df = df.drop(idx, <level and axis info>)
        new_store[key] = df
    new_store.close()

新的 hdf5 文件最终大小约为原始文件的 10%。文件中唯一的区别是所有 NaN 值不再相等(但都是 numpy float64 值)。

我的问题是,如何在现有 hdf5 文件上实现文件大小减小(大概通过管理 NaN 值)?有时我不需要执行上述程序,但无论如何我都会这样做以获得减少。是否有现有的 Pandas 或 PyTables 命令可以执行此操作?预先非常感谢您。

最佳答案

请参阅文档 here

警告说明了一切:

Warning Please note that HDF5 DOES NOT RECLAIM SPACE in the h5 files automatically. Thus, repeatedly deleting (or removing nodes) and adding again WILL TEND TO INCREASE THE FILE SIZE. To clean the file, use ptrepack

关于python - Pandas 和 HDF5 中的文件大小减小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33391854/

相关文章:

python - 如何为 pandas 条形图上的负值和正值着色?

python - 寻找有条件的顺序模式

excel - Matplotlib:直接从 .csv 导入并绘制带有图例的多个时间序列

python - 替换 pandas.datetime 列中的年份

python - pandas.DatetimeIndex.snap 时间戳左出现频率

python - 获取 Pandas 中不包括标题的数据框行

python - 使用 pandas 创建多索引

python - 为什么使用 Flask Mail 发送的文本附件是空的?

python - 确定使用哪个包管理器来安装 Python 包

python - 索引错误 : index 2 is out of bounds for axis 0 with size 2