Reputation: 2507
I would like to sum certain rows based on a condition in a different row.
So I have a columns for points
{'secondBoxer1': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
'secondBoxer2': {0: 0.0, 1: 0.0, 2: 10.0, 3: 0.0, 4: 0.0},
'secondBoxer3': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
'secondBoxer4': {0: 15.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
'secondBoxer5': {0: 15.0, 1: 53.57142857142857, 2: 0.0, 3: 0.0, 4: 0.0},
'secondBoxer6': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0},
'secondBoxer7': {0: 0.0, 1: 0.0, 2: 0.0, 3: 50.0, 4: 0.0},
'secondBoxer8': {0: 0.0, 1: 0.0, 2: 0.0, 3: 37.142857142857146, 4: 0.0}}
and a column with the outcome of each fight
{'outcome1': {0: 'win ', 1: 'win ', 2: 'win ', 3: 'draw ', 4: 'win '},
'outcome2': {0: 'win ', 1: 'win ', 2: 'win ', 3: 'win ', 4: 'win '},
'outcome3': {0: 'win ', 1: 'win ', 2: 'win ', 3: 'win ', 4: 'scheduled '},
'outcome4': {0: 'win ', 1: 'win ', 2: 'nan', 3: 'loss ', 4: 'nan'},
'outcome5': {0: 'win ', 1: 'draw ', 2: 'nan', 3: 'win ', 4: 'nan'},
'outcome6': {0: 'nan', 1: 'nan', 2: 'nan', 3: 'loss ', 4: 'nan'},
'outcome7': {0: 'nan', 1: 'nan', 2: 'nan', 3: 'loss ', 4: 'nan'},
'outcome8': {0: 'nan', 1: 'nan', 2: 'nan', 3: 'win ', 4: 'nan'}}
I would like to sum the points in the first columns (points columns) in cases where the outcome is equals to a win.
I have written this code, where opp_names is the list of columns with the points and outcome_cols is a list of columns with the outcomes
data[opp_names].sum(axis=1).where(data[outcome_cols] == 'win')
The problem with the output from this code is that it returns a total sum of points that is not conditional
Upvotes: 0
Views: 42
Reputation: 323226
In your case we use mask
:d
is your first dict , d1
is your 2nd dict
pd.DataFrame(d).mask(pd.DataFrame(d1).ne('win ').to_numpy()).sum(1)
Out[164]:
0 30.000000
1 0.000000
2 10.000000
3 37.142857
4 0.000000
dtype: float64
Upvotes: 1