How can I calculate a group-wise percentage in pandas?
similar to
Pandas: .groupby().size() and percentages or Pandas Very Simple Percent of total size from Group by I want to calculate the percentage of a value per group.
How can I achieve this?
My dataset is structured like
ClassLabel, Field
Initially, I aggregate on both ClassLbel
and Field
like
grouped = mydf.groupby(['Field', 'ClassLabel']).size().reset_index()
grouped = grouped.rename(columns={0: 'customersCountPerGroup'})
Now I would like to know the percentage of customers in each group on a per group basis. The groups total can be obtained like mydf.groupby(['Field']).size()
but I neither can merge that as a column nor am I sure this is the right approach - there must be something simpler.
edit
I want to calculate the percentage only based on a single group e.g. 3 0 0.125 1 0.250 the sum of 0 + 1 --> 0.125 + 0.250 = 0,375 and use this value to devide / normalize grouped and not grouped.sum()
IIUC you can use:
mydf = pd.DataFrame({'Field':[1,1,3,3,3],'ClassLabel':[4,4,4,4,4],'A':[7,8,9,5,7]})print (mydf)A ClassLabel Field
0 7 4 1
1 8 4 1
2 9 4 3
3 5 4 3
4 7 4 3grouped = mydf.groupby(['Field', 'ClassLabel']).size()
print (grouped)
Field ClassLabel
1 4 2
3 4 3
dtype: int64print (100 * grouped / grouped.sum())
Field ClassLabel
1 4 40.0
3 4 60.0
dtype: float64
grouped = mydf.groupby(['Field', 'ClassLabel']).size().reset_index()
grouped = grouped.rename(columns={0: 'customersCountPerGroup'})
print (grouped)Field ClassLabel customersCountPerGroup
0 1 4 2
1 3 4 3grouped['per'] = 100 * grouped.customersCountPerGroup / grouped.customersCountPerGroup.sum()
print (grouped)Field ClassLabel customersCountPerGroup per
0 1 4 2 40.0
1 3 4 3 60.0
EDIT by comment:
mydf = pd.DataFrame({'Field':[1,1,3,3,3,4,5,6],'ClassLabel':[0,0,0,1,1,0,0,6],'A':[7,8,9,5,7,5,6,4]})print (mydf)grouped = mydf.groupby(['Field', 'ClassLabel']).size()
df = grouped / grouped.sum()df = (grouped / df.groupby(level=0).transform('sum')).reset_index(name='new')
print (df)Field ClassLabel new
0 1 0 8.000000
1 3 0 2.666667
2 3 1 5.333333
3 4 0 8.000000
4 5 0 8.000000
5 6 6 8.000000