swap ks
swap ks

Reputation: 17

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

Upvotes: 2

Views: 62

Answers (2)

ansev
ansev

Reputation: 30930

Use:

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

Upvotes: 2

Quang Hoang
Quang Hoang

Reputation: 150805

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

Upvotes: 1

Related Questions