简体   繁体   中英

Groupby Percentage from total

I am trying to group by the region and finding the percentage of product1 , product2, and product3 from the total product.

Here is the code

a = df_2018.groupby(['ISIC4_ARABIC']).agg({'product1': ['sum'], 'product2': ['sum'], 'product3': ['sum']})

So in the end i will have product1 , product2, and product3 as percentage of total product and the total product as the number.

below is the image of the data frame

Use:

cols=['produc1','product2','product3']
a[cols]=a[cols]/a[cols].sum(axis=1)

IIUC:

s = a.iloc[:, 1:]
a.iloc[:, 1:] = s.div(s.sum(1), axis='rows')

Output ( a ):

  region  product1  product2  product3
0     CA  0.333333  0.333333  0.333333
1     MN  0.500000  0.250000  0.250000
2     OH  1.000000  0.000000  0.000000
3     NY  0.714286  0.142857  0.142857

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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