2016-06-28 2 views
2

여러 개체를 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({'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 

답변

2

이 시도 :

my_store['/data_source_1/part-1'] = ... 

데모 :

In [13]: store = pd.HDFStore('c:/temp/stocks.h5') 

In [15]: store['/aaa/bbb'] = df 

In [17]: store.groups 
Out[17]: 
<bound method HDFStore.groups of <class 'pandas.io.pytables.HDFStore'> 
File path: c:/temp/stocks.h5 
/aaa/bbb   frame  (shape->[3,7]) 
/stocks    wide_table (typ->appendable,nrows->6,ncols->3,indexers->[major_axis,minor_axis],dc->[AAPL,ABC,GOOG])> 

In [18]: store['/aaa/bbb2'] = df 

In [20]: store.items 
Out[20]: 
<bound method HDFStore.items of <class 'pandas.io.pytables.HDFStore'> 
File path: c:/temp/stocks.h5 
/aaa/bbb    frame  (shape->[3,7]) 
/aaa/bbb2   frame  (shape->[3,7]) 
/stocks    wide_table (typ->appendable,nrows->6,ncols->3,indexers->[major_axis,minor_axis],dc->[AAPL,ABC,GOOG])> 

UPDATE :

In [29]: store.get_node('/aaa') 
Out[29]: 
/aaa (Group) '' 
    children := ['bbb' (Group), 'bbb2' (Group)] 

PS AFAIK 팬더가 0,123,325을 고려(/aaa/bbb) 전체 경로로

UPDATE2 :

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]) 

/data_source_1/subdir2의 모든 항목을 찾을 수 있습니다 : 우리는 다음과 같은 상점이있다

: 가게를 나열

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] 
+0

그룹 'aaa'의 모든 요소를 ​​어떻게 나열합니까? – mgoldwasser

+0

@mgoldwasser, 업데이트를 참조하십시오 - 당신이 찾고있는 것이 무엇입니까? – MaxU

+0

도움이 될 것입니다. 아이들을 통해 반복하고 싶다면 my_store.get_node ('/ data_source_1')의 자식에 대해이 [my_store.get ('/ data_source_1 /'+ child)와 같은 작업을 수행 할 수 있다고 생각합니다. _v_children.keys()]'하지만 아마도 더 좋은 방법이 있습니다 ... – mgoldwasser