2017-07-27 86 views
0

我有一個大的矩陣(15000行x 2500列)使用PyTables存儲並獲取如何遍歷行的列迭代。在documentation我只看到如何通過名稱手動訪問每行。如何使用PyTable迭代列名稱?

我有這樣的列:

  • 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() 

如果我只是重複「在排山坳」什麼也沒有發生。由於代碼的正上方,我得到一個堆棧:

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

您可以按名稱每一行中訪問列值:

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

遍歷所有列:

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

是,那部分很簡單。但是我有成千上萬的列,想迭代它們來檢查它們的值,並選擇性地打印列名和它們的值(如果它們落在一定範圍內)。手動訪問它們的名字不會有幫助。 – Jorvis

+0

謝謝!編輯後,我看到你添加了迭代。我被困在想你可以從選定的行中訪問它。這工作。 – Jorvis