如何使用PyTable迭代列名?

时间:2017-07-27 16:34:00

标签: python-3.x pytables

我有一个使用PyTables存储的大矩阵(15000行x 2500列),并了解如何迭代行的列。在documentation我只看到如何通过名称手动访问每一行。

我有以下列:

  • ID
  • X20160730_Day10_123a_2
  • X20160730_Day10_123b_1
  • X20160730_Day10_123b_2

ID列值是一个字符串,如' 10692.RFX7'但所有其他单元格值都是浮点数。这个选择有效,我可以迭代结果行,但我看不到如何迭代列并检查它们的值:

from tables import *
import numpy

def main():
    h5file = open_file('carlo_seth.h5', mode='r', title='Three-file test')
    table = h5file.root.expression.readout

    condition = '(ID == b"10692.RFX7")'
    for row in table.where(condition):
        print(row['ID'].decode())

        for col in row.fetch_all_fields():
            print("{0}\t{1}".format(col, row[col]))

    h5file.close()

if __name__ == '__main__':
    main()

如果我只是用"迭代行#34;什么都没发生。由于代码在上面,我得到一个堆栈:

10692.RFX7
Traceback (most recent call last):
  File "tables/tableextension.pyx", line 1497, in tables.tableextension.Row.__getitem__ (tables/tableextension.c:17226)
KeyError: b'10692.RFX7'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "tables/tableextension.pyx", line 126, in tables.tableextension.get_nested_field_cache (tables/tableextension.c:2532)
KeyError: b'10692.RFX7'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./read_carlo_pytable.py", line 31, in <module>
    main()
  File "./read_carlo_pytable.py", line 25, in main
    print("{0}\t{1}".format(col, row[col]))
  File "tables/tableextension.pyx", line 1501, in tables.tableextension.Row.__getitem__ (tables/tableextension.c:17286)
  File "tables/tableextension.pyx", line 133, in tables.tableextension.get_nested_field_cache (tables/tableextension.c:2651)
  File "tables/utilsextension.pyx", line 927, in tables.utilsextension.get_nested_field (tables/utilsextension.c:8707)
AttributeError: 'numpy.bytes_' object has no attribute 'encode'
Closing remaining open files:carlo_seth.h5...done

1 个答案:

答案 0 :(得分:2)

您可以在每行中按名称访问列值:

for row in table:
    print(row["10692.RFX7"])

迭代所有列:

names = table.coldescrs.keys()
for row in table:
    for name in names:
        print(name, row[name])