我在数据框中有以下原始数据:
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
我还想要向每个经纪人发送多少钱。并在同一张表中显示。我可以通过键入df.groupby('BROKER').sum()
来获取该信息,但是如何将其作为名为BROKER_TOTAL的列添加到我的数据透视表中?
注意:这个问题很相似,但似乎是在旧版本上,我最好根据自己的情况调整它是不行的:Pandas Pivot tables row subtotals
答案 0 :(得分:1)
您可以为df1
创建MultiIndex.from_arrays
,将concat
创建为pt
,最后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