hdf5文件必须写入
table format(而不是固定格式)
命令可以使用pd.read_hdf的where参数进行查询.
df.to_hdf('/tmp/out.h5', 'results_table', mode='w', data_columns=['A'],
format='table')
或者,将所有列指定为(可查询的)数据列:
df.to_hdf('/tmp/out.h5', 'results_table', mode='w', data_columns=True,
format='table')
然后你可以使用
pd.read_hdf('/tmp/out.h5', 'results_table', where='A in [1,3,4]')
选择值列A为1,3或4的行.例如,
import numpy as np
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2],
'B': [0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1],
'C': [34, 32, 35, 34, 31, 34, 29, 34, 12, 34, 32, 34],
'D': [11, 15, 22, 15, 9, 15, 11, 15, 14, 15, 13, 15]})
df.to_hdf('/tmp/out.h5', 'results_table', mode='w', data_columns=['A'],
format='table')
print(pd.read_hdf('/tmp/out.h5', 'results_table', where='A in [1,3,4]'))
产量
A B C D
0 1 0 34 11
2 3 1 35 22
3 4 1 34 15
5 1 0 34 15
7 3 0 34 15
8 4 1 12 14
10 1 0 32 13
如果你有一个非常长的值列表,那么你可以使用字符串格式组合右边的参数:
where='A in {}'.format(vals)