I have a toy dataset like the one below.
Building Department feature1 feature2
0 A 1 14 28
1 A 1 11 26
2 A 1 29 19
3 A 2 26 28
4 A 2 22 27
5 A 2 20 24
6 A 2 15 14
7 A 2 30 21
8 A 3 30 15
9 A 3 16 29
10 A 3 25 23
11 A 3 26 15
12 A 3 11 11
I want to calculate these variables:
- For every building and department,
Score1which is the average offeature1andfeature2for that department (flatten and do the average, no fancy stuff) - For every building and department,
Score2which is the average offeature1andfeature2which is the average excluding that department (focal group in other words).
So, for the Department1 the Score1 will be calculated based on Department 1 average but Score2 will be calculated based on Department 2 and 3.
Final result:
Building Department Score1 Score2
0 A 1 21.16 21.400
1 A 2 22.70 20.500
2 A 3 20.10 22.125
I could not find any pandas shortcut for such kind of an "exclusion". One possible solution can be loop through groups and calculate it like that, but my data is too big for such a loop.
Any help, hint is appreciated! thanks