Percentage calculation in pivot table pandas with

2019-04-13 15:56发布

问题:

I have a dataset containing several sells register from different vendors, locations, dates, and products. The data set is like this:

local   categoria   fabricante   tipo      consistencia    peso         pacote   ordem vendas_kg
AREA I  SABAO       ASATP        DILUIDO   LIQUIDO         1501 A 2000g PLASTICO 1    10
AREA I  SABAO       TEPOS        DILUIDO   LIQUIDO         1501 A 2000g PLASTICO 1    20
AREA I  SABAO       ASATP        CAPSULA   LIQUIDO         1501 A 2000g PLASTICO 1    20
AREA I  SABAO       TEPOS        CAPSULA   LIQUIDO         1501 A 2000g PLASTICO 1    30
AREA I  SABAO       ASATP        DILUIDO   LIQUIDO         1501 A 2000g PLASTICO 2    20
AREA I  SABAO       TEPOS        DILUIDO   LIQUIDO         1501 A 2000g PLASTICO 2    30
AREA I  SABAO       ASATP        CAPSULA   LIQUIDO         1501 A 2000g PLASTICO 2    20
AREA I  SABAO       TEPOS        CAPSULA   LIQUIDO         1501 A 2000g PLASTICO 2    30
AREA II SABAO       ASATP        DILUIDO   LIQUIDO         1501 A 2000g PLASTICO 1    10
AREA II SABAO       TEPOS        DILUIDO   LIQUIDO         1501 A 2000g PLASTICO 1    15
AREA II SABAO       ASATP        CAPSULA   LIQUIDO         1501 A 2000g PLASTICO 1    25
AREA II SABAO       TEPOS        CAPSULA   LIQUIDO         1501 A 2000g PLASTICO 1    35
AREA II SABAO       ASATP        DILUIDO   LIQUIDO         1501 A 2000g PLASTICO 2    20
AREA II SABAO       TEPOS        DILUIDO   LIQUIDO         1501 A 2000g PLASTICO 2    25
AREA II SABAO       TEPOS        CAPSULA   LIQUIDO         1501 A 2000g PLASTICO 2    20
AREA II SABAO       TEPOS        CAPSULA   LIQUIDO         1501 A 2000g PLASTICO 2    30

I'm pivoting this data set usig the following code:

temp_df = pd.pivot_table(df,index=['local','tipo','ordem'], values=['vendas_kg'] , aggfunc=[np.sum], columns=['fabricante'], fill_values=0, margins=True, margins_name= 'Total')

and I get this output:

                                       sum           sum
                                 vendas_kg     vendas_kg
fabricante                           ASATP         TEPOS      Total
local          tipo      ordem 
AREA I         DILUIDO     1          10            20         30
                           2          20            30         50
               CAPSULA     1          10            20         30
                           2          20            30         50
AREA II        DILUIDO     1          10            15         25
                           2          20            25         45
               CAPSULA     1          25            35         55
                           2          20            30         50

I want to calculate the percentage for each ['ordem'] and for the percentage for each segment.g. ordem,tipo and local, like this:

                                       sum           sum
                                 vendas_kg     vendas_kg
fabricante                           ASATP         TEPOS     % segment  Total
local          tipo      ordem 
AREA I         DILUIDO     1          33%            66%         50%     30
                           2          40%            60%         50%     50
               CAPSULA     1          33%            66%         50%     30
                           2          40%            60%         50%     50
AREA II        DILUIDO     1          40%            60%        31.25%   25
                           2         44.44%         55.56%      47.37%   45
               CAPSULA     1         43.64%         57.36%      53.63%   55
                           2          40%            60%        53.63%   50

So the total sales for AREA I DILUIDO 1 is 30, ASATP sales represent 33% of it and TEPOS 66%, and from the total sales of AREA I 1 the DILUIDOs sales represent 50% and so on.

I also want to compare the sales difference between ['ordem'] e.g. percentage growth of segment and ['fabricante'] and store in a new table like this:

                               % change in   % change in
                                 vendas_kg     vendas_kg   % change in  % change in
fabricante                           ASATP         TEPOS     % segment        Total
AREA I  DILUIDO 1                      0             0          0                 0                 
                2                     +7%           -6%         0                20
                3                      0             0          0                 0
AREA I  CAPSULA 1                      0             0          0                 0 
                2                     +7%           -6%         0                20
                3                      0             0          0                 0
AREA II DILUIDO 1                      0             0          0                 0 
                2                    +4.44%        -4.44%    +16.12%             20
                3                      0             0          0                 0
AREA II CAPSULA 1                      0             0          0                 0  
                2                    -3.64%        +3.64%          0              5
                3                      0             0          0                 0

I'm stuck at this over the last 5 days, I have way more categories in ['fabricante']['tipo'] and ['local'] so it must work for more than two categories in each. Thanks for the help in advance, feel free to contact me in case of doubts.

回答1:

To take the percents:

df_percent = temp_df.iloc[:, [0,1]].apply(lambda x: round(x / x.sum() * 100, 2), axis = 1)

to take the the variation, use diff

df_diff_percent = df_percent.groupby(level=[0,1]).diff().fillna(0)

                    sum
                    vendas_kg
      fabricante    ASATP   TEPOS
local   tipo    ordem       
AREA I  CAPSULA 1   0.00    0.00
                2   0.00    0.00
DILUIDO         1   0.00    0.00
                2   6.67    -6.67
AREA II CAPSULA 1   0.00    0.00
                2   -41.67  41.67