Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pivot tables in Python – how to add percentages and subtotals

How can I easily and subtotals for each level and express the numbers in percentages in Python? I have the data below and I would like to add one column which will express the numbers as the percentage of total values in column or as a percentage of total values for a given level. How can I do that easily?

import pandas as pd
df=pd.DataFrame({'A':['x','y','z','x','y','z'],
                 'B':['one','one','one','two','two','two'],
                 'C':[2,18,2,8,2,18]})
df

table = pd.pivot_table(df, index=['A', 'B'],aggfunc=np.sum)

Is there any other solution than the one proposed here Pandas pivot table Percent Calculations?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

IIUC, you can use a simple division or a division by the sum per group:

table.assign(**{'%total': lambda d: d['C'].div(d['C'].sum()),
                '%level': lambda d: d['C'].div(d.groupby(level=0)['C'].transform('sum')),
               })

Output:

        C  %total  %level
A B                      
x one   2    0.04     0.2
  two   8    0.16     0.8
y one  18    0.36     0.9
  two   2    0.04     0.1
z one   2    0.04     0.1
  two  18    0.36     0.9

NB. The command is chainable, you can pipe it directly after your pivot_table

table = (pd.pivot_table(...)
           .assign(...)
        )
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading