How can I compute the absolute sum with a groupby in pandas?

Viewed 17075

How can I compute the absolute sum with a groupby in pandas?

For example, given the DataFrame:

    Player  Score
0      A    100
1      B   -150
2      A   -110
3      B    180
4      B    125

I would like to have the total score for player A (100+110=210) as well as the total score for player A (150+180+125=455), ignoring the sign of the score.

I can use the following code to compute the sum:

import pandas as pd
import numpy as np

frame = pd.DataFrame({'Player' : ['A', 'B', 'A', 'B', 'B'], 
                      'Score'  : [100, -150, -110, 180, 125]})

print('frame: {0}'.format(frame))

total_scores = frame[['Player','Score']].groupby(['Player']).agg(['sum'])

print('total_scores: {0}'.format(total_scores))

but how can I compute the absolute sum with a groupby?

frame[['Player','Score']].abs().groupby(['Player']).agg(['sum']) unsurprisingly returns:

Traceback (most recent call last):
  File "O:\tests\absolute_count.py", line 10, in <module>
    total_scores = frame[['Player','Score']].abs().groupby(['Player']).agg(['sum'])
  File "C:\Users\dernoncourt\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\generic.py", line 5518, in abs
    return np.abs(self)
TypeError: bad operand type for abs(): 'str'

I don't want to alter the DataFrame.

2 Answers
Related