如何使用 pandas 访问 hdf5 中的嵌套表
How to access nested tables in hdf5 with pandas
我想使用 pandas 从 HDF5 文件中检索 table。
根据我发现的几个参考资料,我尝试使用以下方式打开文件:
df = pd.read_hdf('data/test.h5', g_name),
其中 g_name 是我要检索的对象的路径,即 table TAB1,例如 MAIN/Basic/Tables/TAB1.
g_name检索如下:
def get_all(name):
if 'TAB1' in name:
return name
with h5py.File('data/test.h5') as f:
g_name = f.visit(get_all)
print(g_name)
group = f[g_name]
print(type(group))
我也试过检索对象本身,如上面的代码片段所示,但对象类型是
我如何将其转换为我可以在 pandas 中读取为数据框的内容?
对于第一种情况,我得到以下错误:
"cannot create a storer if the object is not existing "
我不明白为什么找不到对象,如果路径与搜索过程中检索到的路径相同。
我找到了以下解决方案:
hf = h5py.File('data/test.h5')
data = hf.get('MAIN/Basic/Tables/TAB1')
result = data[()]
# This last step just converts the table into a pandas df
df = pd.DataFrame(result)
我想使用 pandas 从 HDF5 文件中检索 table。
根据我发现的几个参考资料,我尝试使用以下方式打开文件:
df = pd.read_hdf('data/test.h5', g_name),
其中 g_name 是我要检索的对象的路径,即 table TAB1,例如 MAIN/Basic/Tables/TAB1.
g_name检索如下:
def get_all(name):
if 'TAB1' in name:
return name
with h5py.File('data/test.h5') as f:
g_name = f.visit(get_all)
print(g_name)
group = f[g_name]
print(type(group))
我也试过检索对象本身,如上面的代码片段所示,但对象类型是 我如何将其转换为我可以在 pandas 中读取为数据框的内容?
对于第一种情况,我得到以下错误: "cannot create a storer if the object is not existing "
我不明白为什么找不到对象,如果路径与搜索过程中检索到的路径相同。
我找到了以下解决方案:
hf = h5py.File('data/test.h5')
data = hf.get('MAIN/Basic/Tables/TAB1')
result = data[()]
# This last step just converts the table into a pandas df
df = pd.DataFrame(result)