You’ll worth refer to syntax to calculate the proportion of a complete inside of teams in pandas:
df['values_var'] / df.groupby('group_var')['values_var'].develop into('sum')
Please see instance displays the right way to worth this syntax in apply.
Instance: Calculate Proportion of General Inside Team
Assume we have now refer to pandas DataFrame that displays the issues scored by way of basketball gamers on numerous groups:
import pandas as pd
#form DataFrame
df = pd.DataFrame({'staff': ['A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B'],
'issues': [12, 29, 34, 14, 10, 11, 7, 36, 34, 22]})
#view DataFrame
print(df)
staff issues
0 A 12
1 A 29
2 A 34
3 A 14
4 A 10
5 B 11
6 B 7
7 B 36
8 B 34
9 B 22
We will be able to worth refer to syntax to form a pristine column within the DataFrame that displays the proportion of general issues scored, grouped by way of staff:
#calculate share of general issues scored grouped by way of staff
df['team_percent'] = df['points'] / df.groupby('staff')['points'].develop into('sum')
#view up to date DataFrame
print(df)
staff issues team_percent
0 A 12 0.121212
1 A 29 0.292929
2 A 34 0.343434
3 A 14 0.141414
4 A 10 0.101010
5 B 11 0.100000
6 B 7 0.063636
7 B 36 0.327273
8 B 34 0.309091
9 B 22 0.200000
The team_percent column displays the proportion of general issues scored by way of that participant inside of their staff.
For instance, gamers on staff A scored a complete of 99 issues.
Thus, the participant within the first row of the DataFrame who scored 12 issues scored a complete of 12/99 = 12.12% of the whole issues for staff A.
In a similar way, the participant in the second one row of the DataFrame who scored 29 issues scored a complete of 29/99 = 29.29% of the whole issues for staff A.
And so forth.
Be aware: You’ll in finding the entire documentation for the GroupBy serve as right here.
Alternative Assets
Please see tutorials provide an explanation for the right way to carry out alternative regular operations in pandas:
Pandas: The right way to Calculate Cumulative Sum by way of Team
Pandas: The right way to Rely Distinctive Values by way of Team
Pandas: The right way to Calculate Form by way of Team
Pandas: The right way to Calculate Correlation By means of Team