0

I have a dataset like this:

Country   Name      Match   Result
US        Martin    Win     3
US        Martin    Lose    1
US        Martin    Draw    5
UK        Luther    Win     5
UK        Luther    Draw    3

I'd like to add two more columns with sum result from Win, Lose and Draw, and percentage of each match like this:

Country   Name      Match   Result  All Percentage
US        Martin    Win     3       8   0.375
US        Martin    Lose    1       8   0.125
US        Martin    Draw    5       8   0.625
UK        Luther    Win     6       10  0.6
UK        Luther    Draw    4       10  0.4

I've already tried using groupby and got result for size total match. However I don't know how to put it in the next column.

Thank you

2
  • 3
    Shouldn't the All column for Martin be 9, not 8 (5 + 3 + 1)? Commented Jan 4, 2017 at 14:51
  • yes my mistake, it should be (5+1+2) .. thank you Commented Jan 24, 2017 at 18:36

1 Answer 1

1

IIUC you need GroupBy.transform, sample DataFrame was changed:

df['All'] = df.groupby(['Country','Name'])['Result'].transform('sum')
df['Percentage'] = df.Result.div(df.All)
print (df)
  Country    Name Match  Result  All  Percentage
0      US  Martin   Win       2    8       0.250
1      US  Martin  Lose       1    8       0.125
2      US  Martin  Draw       5    8       0.625
3      UK  Luther   Win       6   10       0.600
4      UK  Luther  Draw       4   10       0.400
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.