2016-11-10 115 views
1

如何將嵌套字典轉換爲熊貓多索引數據框?將嵌套的Python字典轉換爲多索引熊貓數據框

下面是一個例子:

dct={'outer':{}} 
for i in dct: 
    dct[i]={'middle':{}} 
    for j in dct[i]: 
     dct[i][j]={} 
    for j in dct[i]: 
     dct[i][j]['inner']=10 

print dct 

,輸出:

{'outer': {'middle': {'inner': 10}}} 

我想這在大熊貓數據幀,看起來是這樣的:

outer middle inner value 
       inner2 value 
     middle2 inner value 
outer2 middle inner value 
       inner2 value 
     middle2 inner value 

我知道多索引是做這件事的好方法,但我不知道如何製作數據框。任何人都可以給我一些指點嗎?

回答

1

我認爲你可以使用concatDataFrame.from_dict和最後stack創建由dict comprehension - 但輸出SeriesMultiIndex

dct={'outer':{}, 'outer2':{}} 
for i in dct: 
    dct[i]={'middle':{}, 'middle2':{}} 
    for j in dct[i]: 
     dct[i][j]={} 
    for j in dct[i]: 
     dct[i][j]['inner']=10 
     dct[i][j]['inner2']=20 

print (dct) 
{'outer2': {'middle2': {'inner': 10, 'inner2': 20}, 
'middle': {'inner': 10, 'inner2': 20}}, 
'outer': {'middle2': {'inner': 10, 'inner2': 20}, 
'middle': {'inner': 10, 'inner2': 20}}} 
print (pd.concat({key:pd.DataFrame.from_dict(dct[key],orient='index') 
        for key in dct.keys()})) 
       inner inner2 
outer middle  10  20 
     middle2  10  20 
outer2 middle  10  20 
     middle2  10  20 

df = pd.concat({key:pd.DataFrame.from_dict(dct[key], orient='index') 
       for key in dct.keys()}).stack() 
print (df) 
outer middle inner  10 
       inner2 20 
     middle2 inner  10 
       inner2 20 
outer2 middle inner  10 
       inner2 20 
     middle2 inner  10 
       inner2 20 
dtype: int64 
+0

好極了,非常完美。謝謝 – CiaranWelsh

相關問題