-1
    |  red  | blue  | winner|
    |:------|:-----:|------:|
0   |70.0   |67.0   |Red    |
1   |74.0   |76.0   |Red    |
2   |75.0   |75.0   |Red    |
3   |63.0   |61.0   |Blue   |
4   |68.0   |72.0   |Blue   |

In a pandas DataFrame how would I add up the red and blue columns only when they are the winner which is shown on the winner column.

so in this example

  • red = 70 + 74 + 75
  • and blue = 61 + 72
ThePyGuy
  • 17,779
  • 5
  • 18
  • 45
Danny M
  • 25
  • 4

1 Answers1

0
import pandas as pd

df = pd.DataFrame(
    data={
        'red' : [70.0, 74.0, 75.0, 63.0, 68.0],
        'blue' : [67.0, 76.0, 75.0, 61.0, 72.0],
        'winner' : ['Red', 'Red', 'Red', 'Blue', 'Blue']
    }
)
print(df['red'][df['winner'] == 'Red'].sum())
print(df['blue'][df['winner'] == 'Blue'].sum())
Davinder Singh
  • 2,060
  • 2
  • 7
  • 22