【发布时间】:2016-01-28 06:34:23
【问题描述】:
我正在运行一个模型,该模型将数据输出到多个 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 命令可以做到这一点?非常感谢您。
【问题讨论】:
标签: python numpy pandas hdf5 pytables