I have a pandas dataframe containing the following data:
matchID server court speed
1 1 A 100
1 2 D 200
1 3 D 300
1 4 A 100
1 1 A 120
1 2 A 250
1 3 D 110
1 4 D 100
2 1 A 100
2 2 D 200
2 3 D 300
2 4 A 100
2 1 A 120
2 2 A 250
2 3 D 110
2 4 D 100
I would like to add two new columns containing the mean based on two conditions. The column meanSpeedCourtA13
shall contain the mean speed of servers
1 and 3 where court = A
. This would bee (100 + 120) / 2 = 110
. The second column named meanSpeedCourtD13
shall contain the mean speed of servers
1 and 3 where court = D
. This would be (300 + 110) / 2 = 205
.
Please note that this should be done for each matchID
, hence, a groupby is also required. this means that solutions containing iloc()
cannot be used.
The resulting dataframe should look as follows:
matchID server court speed meanSpeedCourtA13 meanSpeedCourtD13
1 1 A 100 110 205
1 2 D 200 110 205
1 3 D 300 110 205
1 4 A 100 110 205
1 1 A 120 110 205
1 2 A 250 110 205
1 3 D 110 110 205
1 4 D 100 110 205
2 1 A 100 110 205
2 2 D 200 110 205
2 3 D 300 110 205
2 4 A 100 110 205
2 1 A 120 110 205
2 2 A 250 110 205
2 3 D 110 110 205
2 4 D 100 110 205