2017-07-27 12 views
0

PyTablesを使って大きな行列(15000行×2500列)を保存し、行の列を繰り返し処理する方法を見ています。 documentationでは、各行を手動で名前でアクセスする方法しか見ていません。 PyTableで列名を反復処理する方法は?

ID列の値が '10692.RFX7' は、他のすべてのような文字列である

  • ID
  • X20160730_Day10_123a_2
  • X20160730_Day10_123b_1
  • X20160730_Day10_123b_2:

    は、私のような列を持っていますセル値は浮動小数点数です。この選択作品と私は、結果の行を反復処理することができますが、私は列を反復し、その値を確認する方法を見ることができません。

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

    私は「行にCOLのために」と繰り返す場合は何も起こりません。上記のコードがあるので、私はスタックを取得:

    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

関連する問題