Python pandas使用read-hdf和HDFStore.s从HDF5文件读取特定值

2024-04-28 14:46:38 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我用一个简单的数据集创建了hdf5文件,如下所示

>>> pd.read_hdf('STORAGE2.h5', 'table')
   A  B
0  0  0
1  1  1
2  2  2
3  3  3
4  4  4

使用此脚本

import pandas as pd
import scipy as sp
from pandas.io.pytables import Term

store = pd.HDFStore('STORAGE2.h5')

df_tl = pd.DataFrame(dict(A=list(range(5)), B=list(range(5))))

df_tl.to_hdf('STORAGE2.h5','table',append=True)

我知道我可以用

x = pd.read_hdf('STORAGE2.h5', 'table',  columns=['A'])

或者

x = store.select('table', where = 'columns=A')

如何选择“A”列中等于3的所有值,或“A”列中带字符串的特定值或指示符,如“foo”?在pandas数据帧中,我将使用df[df["A"]==3]df[df["A"]=='foo']

如果我使用read_hdf()store.select(),效率是否也会有所不同?


Tags: 数据storeimportpandasdfreadastable