2017-04-01 57 views
3

考慮:如何將字典轉換爲多級數據框?

A = pd.DataFrame([[1, 5, 2, 8, 2], [2, 4, 4, 20, 2], [3, 3, 1, 20, 2], [4, 2, 2, 1, 0], 
       [5, 1, 4, -5, -4], [1, 5, 2, 2, -20], [2, 4, 4, 3, 0], [3, 3, 1, -1, -1], 
       [4, 2, 2, 0, 0], [5, 1, 4, 20, -2]], 
      columns=['a', 'b', 'c', 'd', 'e'], 
      index=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) 

B = pd.DataFrame([[0, 0, 0, 8, 2], [1, 1, 1, 1, 1], [0, 0, 0, 8, 2], [0, 0, 2, 1, 0], 
       [5, 1, 4, -5, -4], [0, 0, 0, 8, 2], [2, 4, 4, 3, 0], [1, 3, 1, -1, -1], 
       [1, 1, 2, 0, 0], [2, 2, 2, 20, -2]], 
      columns=['a', 'b', 'c', 'd', 'e'], 
      index=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) 

test_list = [('test1', A), ('test2', B)] 
d_test = dict(test_list) 

然後,我遇到麻煩時,我嘗試這本字典轉換成一個多層次的數據幀。但我不知道如何指定多級別,我希望'key'是第二級數據框的名稱。這是所需的輸出

df 

    test_1    test_2 
    a b c d e a b c d e 
1 1 5 2 8 2 0 0 0 8 2 
2 2 4 4 20 2 1 1 1 1 1 
3 3 3 1 20 2 0 0 0 8 2 
4 4 2 2 1 0 0 0 2 1 0 
5 5 1 4 -5 -4 5 1 4 -5 -4 
6 1 5 2 2 -20 0 0 0 8 2 
7 2 4 4 3 0 2 4 4 3 0 
8 3 3 1 -1 -1 1 3 1 -1 -1 
9 4 2 2 0 0 1 1 2 0 0 
10 5 1 4 20 -2 2 2 2 20 -2 
+0

你需要一本字典嗎? –

+0

此外,如果索引不是「B」或「A」會發生什麼? –

+0

@WillemVanOnsem是的,除非從列表中更容易做到,否則我需要評估是否更改其餘代碼以適應此..... \\索引是相同的。 – hernanavella

回答

1

不僅僅是因爲這裏的答案 (其已經在評論中給出)的目的,這裏有雲(再次):

import pandas as pd 
A = pd.DataFrame([[1, 5, 2, 8, 2], [2, 4, 4, 20, 2], [3, 3, 1, 20, 2], [4, 2, 2, 1, 0], 
       [5, 1, 4, -5, -4], [1, 5, 2, 2, -20], [2, 4, 4, 3, 0], [3, 3, 1, -1, -1], 
       [4, 2, 2, 0, 0], [5, 1, 4, 20, -2]], 
      columns=['a', 'b', 'c', 'd', 'e'], 
      index=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) 
B = pd.DataFrame([[0, 0, 0, 8, 2], [1, 1, 1, 1, 1], [0, 0, 0, 8, 2], [0, 0, 2, 1, 0], 
       [5, 1, 4, -5, -4], [0, 0, 0, 8, 2], [2, 4, 4, 3, 0], [1, 3, 1, -1, -1], 
       [1, 1, 2, 0, 0], [2, 2, 2, 20, -2]], 
      columns=['a', 'b', 'c', 'd', 'e'], 
      index=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) 
AB=pd.concat([A,B], axis=1) 
header = ['test1','test1','test1','test1','test1','test2','test2','test2','test2','test2'] 
AB.columns = pd.MultiIndex.from_tuples(list(zip(header, AB.columns))) 
print(AB) # gives what was asked for 
print("test1: \n", AB.test1) # gives A 
print("test2: \n", AB.test2) # gives B 

另一個(下降到實生活狀況)實現上述意見給予相同的(答案)的方式:

test_list = [('test1', A), ('test2', B)] 
d_test = dict(test_list) 
AB = pd.concat(d_test.values(), keys=d_test.keys(), axis=1) 
# what means: AB = pd.concat([A,B], keys=['test1', 'test2'], axis=1) 

上述輸出碼:

test1    test2    
     a b c d e  a b c d e 
1  1 5 2 8 2  0 0 0 8 2 
2  2 4 4 20 2  1 1 1 1 1 
3  3 3 1 20 2  0 0 0 8 2 
4  4 2 2 1 0  0 0 2 1 0 
5  5 1 4 -5 -4  5 1 4 -5 -4 
6  1 5 2 2 -20  0 0 0 8 2 
7  2 4 4 3 0  2 4 4 3 0 
8  3 3 1 -1 -1  1 3 1 -1 -1 
9  4 2 2 0 0  1 1 2 0 0 
10  5 1 4 20 -2  2 2 2 20 -2 
test1: 
    a b c d e 
1 1 5 2 8 2 
2 2 4 4 20 2 
3 3 3 1 20 2 
4 4 2 2 1 0 
5 5 1 4 -5 -4 
6 1 5 2 2 -20 
7 2 4 4 3 0 
8 3 3 1 -1 -1 
9 4 2 2 0 0 
10 5 1 4 20 -2 
test2: 
    a b c d e 
1 0 0 0 8 2 
2 1 1 1 1 1 
3 0 0 0 8 2 
4 0 0 2 1 0 
5 5 1 4 -5 -4 
6 0 0 0 8 2 
7 2 4 4 3 0 
8 1 3 1 -1 -1 
9 1 1 2 0 0 
10 2 2 2 20 -2