简体   繁体   English

为数据透视表中的不同列的行创建小计

[英]Create subtotals for rows in pivot table for different columns

I am creating a pivot table with Pandas but got stuck at the subtotals for rows within different columns (under the same Values) for a while, I've done some research on stackoverflow such as Pandas Pivot tables row subtotals and this Subtotals for Pandas pivot table index and column (actually this one is close to but some what also different from my case) but didn't find right hint for my case, so please for this topic, thanks. 我正在用Pandas创建数据透视表,但卡在不同列(相同值下)中的行的小计有一段时间,我对stackoverflow进行了一些研究,例如Pandas Pivot表行小计和此Pandas数据小 计表索引和列 (实际上这是一个接近但与我的情况有些不同的地方),但没有找到适合我的情况的提示,因此请提供有关本主题的信息,谢谢。

Here I will provide a simplified DataFrame (original one is too big to show here, so their values really do not matter, the format is what I am looking for) of my case and the code I wrote to get my pivot table. 在这里,我将提供我的情况和为获取数据透视表而编写的代码的简化DataFrame(原始数据框太大,无法在此处显示,因此它们的值确实无关紧要,格式是我想要的格式)。

import pandas as pd
import numpy as np
df = pd.DataFrame(
{
    'Co':['NN']*6,
    'Op':['A','B']*3,
    'Stk':[1.1,1.2,1.3]*2,
    'Tm':['07-01-2018','08-01-2018','09-01-2018']*2,
    'Qt':[100,200,300]*2
}
)
df

The df output looks like: df输出看起来像:

Co  Op  Qt  Stk Tm
0   NN  A   100 1.1 07-01-2018
1   NN  B   200 1.2 08-01-2018
2   NN  A   300 1.3 09-01-2018
3   NN  B   100 1.1 07-01-2018
4   NN  A   200 1.2 08-01-2018
5   NN  B   300 1.3 09-01-2018

Then I convert above df to my pivot table by: 然后我通过以下方式将df上方转换为我的数据透视表:

df['Qt'] = df['Qt'].apply(pd.to_numeric)
df['Stk']=df['Stk'].apply(pd.to_numeric)
df['Co'] = df['Co'].astype(str)
tb=pd.pivot_table(df,index=["Tm"],columns=["Co","Op","Stk"],aggfunc=np.sum,values=['Qt'], fill_value=0, margins=True, margins_name='All')
tb

The generated pivot table looks like: 生成的数据透视表如下所示:

            Qt
Co          NN                              All
Op          A              B    
Stk         1.1  1.2  1.3  1.1  1.2  1.3    
        Tm                          
07-01-2018  100  0    0    100  0    0      200
08-01-2018  0    200  0    0    200  0      400
09-01-2018  0    0    300  0    0    300    600
       All  100  200  300  100  200  300    1200

The format which I really expect is: 我真正期望的格式是:

            Qt
Co          NN                                                All
Op          A              ATotal   B               BTotal
Stk         1.1  1.2  1.3           1.1  1.2  1.3   
        Tm                          
07-01-2018  100  0    0    100      100  0    0     100       200
08-01-2018  0    200  0    200      0    200  0     200       400
09-01-2018  0    0    300  300      0    0    300   300       600
       All  100  200  300  600      100  200  300   600       1200

I was trying to create this exact same format for a while and still can't get the same one (I tried something like creating two separate A and B pivot tables and union them together, but it will mess with the All margins). 我曾尝试过一段时间创建这种完全相同的格式,但仍然无法获得相同的格式(我尝试过类似创建两个单独的A和B数据透视表并将它们合并在一起的操作,但会与All margin混淆)。 So help is really needed here. 因此,这里确实需要帮助。 ps I am still new to stackoverflow community, so please pardon if my question is missing some aspects, thank you. ps我还是stackoverflow社区的新手,所以如果我的问题缺少某些方面,请原谅,谢谢。

pivot_table doesn't support it, but you can compute it yourself and concatenate it later: pivot_table不支持它,但是您可以自己计算它,然后将其连接起来:

tb.groupby(level='Op', axis=1).sum().add_suffix('Total')

Op          Total  ATotal  BTotal
Tm                               
07-01-2018    200     100     100
08-01-2018    400     200     200
09-01-2018    600     300     300
All          1200     600     600

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM