2013-07-16 75 views
3

另一個新手熊貓問題。我想將一個DataFrame轉換爲一個字典,但方式不同於DataFrame.to_dict()函數提供的方式。說明通過示例:熊貓:用MultiIndex轉換數據幀爲字典

df = pd.DataFrame({'co':['DE','DE','FR','FR'], 
        'tp':['Lake','Forest','Lake','Forest'], 
        'area':[10,20,30,40], 
        'count':[7,5,2,3]}) 
df = df.set_index(['co','tp']) 

之前:

  area count 
co tp 
DE Lake  10  7 
    Forest 20  5 
FR Lake  30  2 
    Forest 40  3 

後:

{('DE', 'Lake', 'area'): 10, 
('DE', 'Lake', 'count'): 7, 
('DE', 'Forest', 'area'): 20, 
... 
('FR', 'Forest', 'count'): 3 } 

的字典的鍵應該是包括索引行+列標題的元組,而字典值應各個DataFrame值。對於上面的例子,我設法找到這個表達式:

after = {(r[0],r[1],c):df.ix[r,c] for c in df.columns for r in df.index} 

我怎樣才能概括這個代碼MultiIndices有n個級別(而不是2)工作?

回答

感謝DSM's answer,我發現,我其實只需要使用元組串聯r+(c,)和我上面的2維環變成N維:

after = {r + (c,): df.ix[r,c] for c in df.columns for r in df.index} 

回答

7

如何:

>>> df 
      area count 
co tp     
DE Lake  10  7 
    Forest 20  5 
FR Lake  30  2 
    Forest 40  3 
>>> after = {r + (k,): v for r, kv in df.iterrows() for k,v in kv.to_dict().items()} 
>>> import pprint 
>>> pprint.pprint(after) 
{('DE', 'Forest', 'area'): 20, 
('DE', 'Forest', 'count'): 5, 
('DE', 'Lake', 'area'): 10, 
('DE', 'Lake', 'count'): 7, 
('FR', 'Forest', 'area'): 40, 
('FR', 'Forest', 'count'): 3, 
('FR', 'Lake', 'area'): 30, 
('FR', 'Lake', 'count'): 2} 
+0

謝謝,'r +(k,)'-idea丟失了。有了它,我甚至可以使用我的原始索引/列循環,這看起來更容易閱讀。 – ojdo