本文介绍了使用 MultiIndex 计算 pandas pivot_table 中的小计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据框中有以下原始数据:

I have the following raw data, in a dataframe:

   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:

I want to do some summarization of the data to see how much each broker sent to each venue, so I created a 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 的列添加到我的数据透视表中?

I also want how much was sent to each broker overall. and show it in this same table. I can get that information by typing df.groupby('BROKER').sum(), but how can I add this to my pivot table as a column named, say, BROKER_TOTAL?

注意:这个问题很相似,但似乎是在旧版本上,我对使其适应我的情况的最佳猜测不起作用:Pandas 数据透视表行小计

Note: This question is similar but seems to be on an older version, and my best guess at adapting it to my situation didn't work: Pandas Pivot tables row subtotals

推荐答案

您可以创建 MultiIndex.from_arrays 用于 df1concatpt 并最后 sort_index:

You can create MultiIndex.from_arrays for df1, concat it to pt and last 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

这篇关于使用 MultiIndex 计算 pandas pivot_table 中的小计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 14:38