Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Pandas average based on condition into new column

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        
like image 589
beta Avatar asked Dec 31 '17 00:12

beta


1 Answers

Ok this got a bit more complicated. Normally I'd try something with transform but I'd be glad if someone had something better than the following:

Use groupby and send df to func where df.loc is used, lastly use pd.concat to glue the dataframe together again:

import pandas as pd

data = {'matchID': {0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 2, 9: 2, 10: 2, 
                    11: 2, 12: 2, 13: 2, 14: 2, 15: 2}, 
'court': {0: 'A', 1: 'D', 2: 'D', 3: 'A', 4: 'A', 5: 'A', 6: 'D', 7: 'D', 8: 'A',
          9: 'D', 10: 'D', 11: 'A', 12: 'A', 13: 'A', 14: 'D', 15: 'D'}, 
'speed': {0: 100, 1: 200, 2: 300, 3: 100, 4: 120, 5: 250, 6: 110, 7: 100, 8: 100, 
          9: 200, 10: 300, 11: 100, 12: 120, 13: 250, 14: 110, 15: 100}, 
'server': {0: 1, 1: 2, 2: 3, 3: 4, 4: 1, 5: 2, 6: 3, 7: 4, 8: 1, 9: 2, 10: 3, 
           11: 4, 12: 1, 13: 2, 14: 3, 15: 4}}

df = pd.DataFrame(data)

def func(dfx):
    dfx['meanSpeedCourtA13'],dfx['meanSpeedCourtD13'] = \
     (dfx.loc[(dfx.server.isin((1,3))) & (dfx.court == 'A'),'speed'].mean(),
      dfx.loc[(dfx.server.isin((1,3))) & (dfx.court == 'D'),'speed'].mean())
    return dfx

newdf = pd.concat(func(dfx) for _, dfx in df.groupby('matchID'))

print(newdf)

Returns

   court  matchID  server  speed  meanSpeedCourtA13  meanSpeedCourtD13
0      A        1       1    100             110.00             205.00
1      D        1       2    200             110.00             205.00
2      D        1       3    300             110.00             205.00
3      A        1       4    100             110.00             205.00
4      A        1       1    120             110.00             205.00
5      A        1       2    250             110.00             205.00
6      D        1       3    110             110.00             205.00
7      D        1       4    100             110.00             205.00
8      A        2       1    100             110.00             205.00
9      D        2       2    200             110.00             205.00
10     D        2       3    300             110.00             205.00
11     A        2       4    100             110.00             205.00
12     A        2       1    120             110.00             205.00
13     A        2       2    250             110.00             205.00
14     D        2       3    110             110.00             205.00
15     D        2       4    100             110.00             205.00
like image 169
Anton vBR Avatar answered Oct 26 '22 23:10

Anton vBR