我创建了一个多维 pandas 数据框,如下所示:
import numpy as np
import pandas as pd
iterables = [['bar', 'baz', 'foo', 'qux'], ['one', 'two']]
mindex = pd.MultiIndex.from_product(iterables, names=['first', 'second'])
df = pd.DataFrame(np.random.randn(8, 4), index=mindex)
store = pd.HDFStore("df.h5")
store["df"] = df
store.close()
我想添加属性df
存储在 HDFStore 中。我怎样才能做到这一点?好像没有文档 https://pandas.pydata.org/pandas-docs/stable/api.html#hdfstore-pytables-hdf5关于属性以及用于存储属性的组df
与 HDF5 组的类型不同h5py
模块:
type(list(store.groups())[0])
Out[24]: tables.group.Group
似乎是 pytables 组,只有这个涉及其他类型属性的私有成员函数:
__setattr__(self, name, value)
| Set a Python attribute called name with the given value.
我想要的是简单地存储一堆具有多维索引的 DataFrame,这些索引以结构化方式由属性“标记”,以便我可以比较它们并根据这些属性对它们进行子选择。
基本上 HDF5 的用途是用于 pandas 的 + multidim DataFrames。
有这样的问题this one https://stackoverflow.com/questions/33451926/read-hdf5-file-to-pandas-dataframe-with-conditions,它们处理使用 pandas 以外的其他读取器读取 HDF5 文件,但它们都有带有一维索引的 DataFrame,这使得简单地转储 numpy ndarrays 并另外存储索引变得很容易。