2017-01-09 69 views
1

我有字典的兩本詞典這樣:MultiIndexing兩名詞典作爲大熊貓據幀

d_old = {person1: {a1:aaa, b1:bbb, c1:ccc...}, person2:{...}, ...} 
d_new = {person1: {a1:AAA, b1:BBB, c1:CCC...}, person2:{...}, ...} 

我想這些對準彼此相鄰的熊貓數據幀,使用一分級MultiIndexing創造的東西是這樣的:

    d_old d_new 
person1 a1 aaa AAA 
      b1 bbb BBB 
      c1 ccc CCC 
person2 a1 ... 

我是新來的大熊貓,而我能得到多索引工作了一個字典,我在與試圖對準這兩個損失。怎麼做?

...另外,我無法獲得上述表格的格式,如果您知道如何將這個問題編輯成更具可讀性的表格。

回答

3

您可以使用concatunstackDataFrames

d_old = {'person1': {'a1':'aaa', 'b1':'bbb', 'c1':'ccc'}, 
     'person2':{'a1':'cc', 'b1':'dd', 'c1':'ee'},} 
d_new = {'person1': {'a1':'AAA', 'b1':'BBB', 'c1':'CCC'}, 
     'person2':{'a1':'AAA', 'b1':'BBB', 'c1':'CCC'}} 

df1 = pd.DataFrame(d_old) 
df2 = pd.DataFrame(d_new) 
print(df1) 
print(df2) 

print (pd.concat([df1.unstack(), df2.unstack()], keys=['df_old','df_new'], axis=1)) 
      df_old df_new 
person1 a1 aaa AAA 
     b1 bbb BBB 
     c1 ccc CCC 
person2 a1  cc AAA 
     b1  dd BBB 
     c1  ee CCC