【问题标题】:Append all rows from one table to another using Pytables使用 Pytables 将一个表中的所有行附加到另一个表
【发布时间】:2014-09-13 11:42:59
【问题描述】:

我们以以下为例:

import tables
import numpy as np

# Two Example Tables
hfile = tables.open_file('myfile.h5', 'a')
data1 = np.ones((3, 2))
data1.dtype = [('a', float), ('b', float)]
data2 = np.zeros((3, 2))
data2.dtype = [('a', float), ('b', float)]

table1 = hfile.create_table(where='/', name='table1', obj=data1)
table2 = hfile.create_table(where='/', name='table2', obj=data2)

# Appending
table1.append(table2.read())
table2.remove()

hfile.flush()
hfile.close()

有没有更好的方法在磁盘上执行此操作?

一种解决方案是遍历行:

for r in table2.iterrows():
    table1.append([r[:]])

后者似乎很庞大,而前者在追加之前将整个第二个表绘制到内存中。我宁愿做这样的事情:

table2.append_where(dstTable=table1)

但是这个函数只适用于一个条件,所以我需要一个总是评估为真的函数来获取整个表。肯定有更好的办法。

【问题讨论】:

    标签: python database hdf5 pytables


    【解决方案1】:

    我认为append_where() 带有像'True' 这样的微不足道的条件可能是最好的解决方案。

    【讨论】:

    • 我尝试用一​​个简单的条件 ('True') 来完成此操作并收到以下错误:ValueError: there are no columns taking part in condition 'True'
    • 嗯,那么您只需要一些像col1 == col1 这样的琐碎条件,假设没有 NaN 就可以工作。或者如果有 NaN,你可以使用~(col1 != col1)。这应该是 PyTables 的一个功能,不过,您介意向列表或 github 提交功能请求吗?
    【解决方案2】:

    我为 PyTables 创建了一个pull request 以使条件可选,正如@jtorca 要求的那样。鉴于support voiced by one of the maintainers,它很可能会被接受并包含在 PyTables 的未来版本中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-27
      • 2020-02-02
      • 1970-01-01
      • 1970-01-01
      • 2018-01-11
      • 2021-03-17
      • 2020-06-05
      • 2020-02-18
      相关资源
      最近更新 更多