Python 在HDFStore组中存储多个对象
我想在HDFStore中存储多个对象,但我想通过分组来组织它。大致如下:Python 在HDFStore组中存储多个对象,python,pandas,hdf5,hdfstore,Python,Pandas,Hdf5,Hdfstore,我想在HDFStore中存储多个对象,但我想通过分组来组织它。大致如下: import pandas as pd my_store = pd.HDFStore('my_local_store.h5') my_store._handle.createGroup('/', 'data_source_1') # this works, but I'm not sure what it does my_store['/data_source_1']['part-1'] = pd.DataFrame({'
import pandas as pd
my_store = pd.HDFStore('my_local_store.h5')
my_store._handle.createGroup('/', 'data_source_1') # this works, but I'm not sure what it does
my_store['/data_source_1']['part-1'] = pd.DataFrame({'b':[1,2,9,2,3,5,2,5]}) # this does not work
my_store['/data_source_1']['part-2'] = pd.DataFrame({'b':[3,8,4,2,5,5,6,1]}) # this does not work either
试试这个:
my_store['/data_source_1/part-1'] = ...
演示:
PS AFAIK Pandas将键
(/aaa/bbb
)视为完整路径
更新2:列出商店:
我们有以下商店:
In [19]: store
Out[19]:
<class 'pandas.io.pytables.HDFStore'>
File path: D:\temp\.data\hdf\test_groups.h5
/data_source_1/subdir1/1 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index])
/data_source_1/subdir1/2 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index])
/data_source_1/subdir1/3 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index])
/data_source_1/subdir1/4 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index])
/data_source_1/subdir1/5 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index])
/data_source_1/subdir2/1 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index],dc->[a,b,c])
/data_source_1/subdir2/2 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index],dc->[a,b,c])
/data_source_1/subdir2/3 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index],dc->[a,b,c])
/data_source_1/subdir2/4 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index],dc->[a,b,c])
/data_source_1/subdir2/5 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index],dc->[a,b,c])
/data_source_1/subdir2/6 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index],dc->[a,b,c])
/data_source_1/subdir2/7 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index],dc->[a,b,c])
/data_source_1/subdir2/8 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index],dc->[a,b,c])
/data_source_1/subdir2/9 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index],dc->[a,b,c])
使用这些键,您可以轻松选择数据:
In [25]: dfs = [store.select(s, where='a > 5') for s in store if s.startswith('/data_source_1/subdir2/')]
In [26]: [len(df) for df in dfs]
Out[26]: [5, 5, 5, 5, 5, 5, 5, 5, 5]
In [29]: dfs = [store.select(s, where='a > 7') for s in store if s.startswith('/data_source_1/subdir2/')]
In [30]: [len(df) for df in dfs]
Out[30]: [4, 4, 4, 4, 4, 4, 4, 4, 4]
然后我将如何列出组“aaa”中的所有元素?@mgoldwasser,请参阅更新-这就是您要查找的内容吗?这很有用-如果我想循环遍历子项,我想我可以为我的子项存储中的子项执行以下操作:
[my_store.get('/data_source_1/'+child)。get_节点('/data_source__1')。\u children.keys()]
但也许有更好的方法…@mgoldwaser,我认为可以做得更简单一些-参见更新2
In [19]: store
Out[19]:
<class 'pandas.io.pytables.HDFStore'>
File path: D:\temp\.data\hdf\test_groups.h5
/data_source_1/subdir1/1 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index])
/data_source_1/subdir1/2 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index])
/data_source_1/subdir1/3 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index])
/data_source_1/subdir1/4 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index])
/data_source_1/subdir1/5 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index])
/data_source_1/subdir2/1 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index],dc->[a,b,c])
/data_source_1/subdir2/2 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index],dc->[a,b,c])
/data_source_1/subdir2/3 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index],dc->[a,b,c])
/data_source_1/subdir2/4 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index],dc->[a,b,c])
/data_source_1/subdir2/5 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index],dc->[a,b,c])
/data_source_1/subdir2/6 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index],dc->[a,b,c])
/data_source_1/subdir2/7 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index],dc->[a,b,c])
/data_source_1/subdir2/8 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index],dc->[a,b,c])
/data_source_1/subdir2/9 frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index],dc->[a,b,c])
In [20]: [s for s in store if s.startswith('/data_source_1/subdir2/')]
Out[20]:
['/data_source_1/subdir2/1',
'/data_source_1/subdir2/2',
'/data_source_1/subdir2/3',
'/data_source_1/subdir2/4',
'/data_source_1/subdir2/5',
'/data_source_1/subdir2/6',
'/data_source_1/subdir2/7',
'/data_source_1/subdir2/8',
'/data_source_1/subdir2/9']
In [25]: dfs = [store.select(s, where='a > 5') for s in store if s.startswith('/data_source_1/subdir2/')]
In [26]: [len(df) for df in dfs]
Out[26]: [5, 5, 5, 5, 5, 5, 5, 5, 5]
In [29]: dfs = [store.select(s, where='a > 7') for s in store if s.startswith('/data_source_1/subdir2/')]
In [30]: [len(df) for df in dfs]
Out[30]: [4, 4, 4, 4, 4, 4, 4, 4, 4]