2016-10-10 108 views
1

我有以下的原始數據,在數據幀:在大熊貓進行分類彙總計算pivot_table用多指標

BROKER VENUE QUANTITY 
0 BrokerA Venue_1  300 
1 BrokerA Venue_2  400 
2 BrokerA Venue_2  1400 
3 BrokerA Venue_3  800 
4 BrokerB Venue_2  500 
5 BrokerB Venue_3  1100 
6 BrokerC Venue_1  1000 
7 BrokerC Venue_1  1200 
8 BrokerC Venue_2  17000 

我想要做一些數據的彙總,看看有多少各家券商發送到每個場館,所以我創建一個pivot_table:

pt = df.pivot_table(index=['BROKER', 'VENUE'], values=['QUANTITY'], aggfunc=np.sum) 

結果,符合市場預期:

    QUANTITY 
BROKER VENUE    
BrokerA Venue_1  300.0 
     Venue_2 1800.0 
     Venue_3  800.0 
BrokerB Venue_2  500.0 
     Venue_3 1100.0 
BrokerC Venue_1 2200.0 
     Venue_2 17000.0 

我也多麼想得瑟nt給每個經紀人整體。並在同一張表中顯示。我可以通過輸入df.groupby('BROKER').sum()來獲取這些信息,但是我怎樣才能將它作爲名爲BROKER_TOTAL的列添加到我的數據透視表中?

注:這個問題是類似的,但似乎是一箇舊版本,並在其適應我的情況我最好的猜測沒有工作:Pandas Pivot tables row subtotals

回答

1

您可以創建MultiIndex.from_arraysdf1concatpt並最後sort_index

df1 = df.groupby('BROKER').sum() 
df1.index = pd.MultiIndex.from_arrays([df1.index + '_total', len(df1.index) * ['']]) 
print (df1) 
       QUANTITY 
BrokerA_total  2900 
BrokerB_total  1600 
BrokerC_total  19200 

print (pd.concat([pt, df1]).sort_index()) 
         QUANTITY 
BROKER  VENUE    
BrokerA  Venue_1  300 
       Venue_2  1800 
       Venue_3  800 
BrokerA_total    2900 
BrokerB  Venue_2  500 
       Venue_3  1100 
BrokerB_total    1600 
BrokerC  Venue_1  2200 
       Venue_2  17000 
BrokerC_total    19200