我有一个非常大的HDF5文件,我想在pandas DataFrame中加载其中一部分以执行某些操作,但我只想筛选出一些行。
以下是一个示例:
原始的HDF5文件可能看起来像这样:
A B C D
1 0 34 11
2 0 32 15
3 1 35 22
4 1 34 15
5 1 31 9
1 0 34 15
2 1 29 11
3 0 34 15
4 1 12 14
5 0 34 15
1 0 32 13
2 1 34 15
etc etc etc etc
我想要做的是将这个内容完全加载到pandas Dataframe中,但只在A等于1、3或4时加载。到目前为止,我只能使用以下方法来加载整个HDF5:
store = pd.HDFStore('Resutls2015_10_21.h5')
df = pd.DataFrame(store['results_table'])
我不知道如何在这里加入where
条件。
h5
文件不是以table
格式保存的,那么使用带有where
参数的pd.read_hdf
将会引发TypeError: cannot pass a where specification when reading from a Fixed format...
错误。 如果h5
文件是采用table
格式保存的,但没有将A
指定为data_column
,那么你将会得到ValueError: The passed where expression: A in [1,3,4] contains an invalid variable reference...
的错误提示。 - unutbufixed
格式的h5文件转换为table
格式,或者添加data_columns
。据我所知,您需要将整个h5
文件读入DataFrame中(或者使用chunksize
参数分块读取),然后将其写出或附加到另一个以table
格式存储的h5
文件中。 - unutbu