2
\$\begingroup\$

This script counts how many matches were won by the team with a first half, second half, and full game time of possession differential advantage.

The data is stored in a pandas data frame:

   Points  H1  H2  Total
0      36  46  50     96
1      -5  16  18     34
2      47  12  65     77
3      12  79  13     92
4      -7  53 -64    -11
5      36  -3 -55    -58
6       9  27  87    114
7      17  66 -10     56

The script counts matches won by a team with a positive time of possession differential as well as matches lost by a team with a negative time of possession differential.

From the above example data, matches 0,2,3,4,6,7 would be selected for total wins because the team with the possession advantage won or the team with the possession disadvantage lost.

Match 1 was lost by the team with more total possession. Match 5 was won by the team with less total possession.

This snippet finds the winners for first half, second half, and the total match time.

total_win = ((poss_diff['Points'] > 0) & (poss_diff['Total'] > 0) | (poss_diff['Points'] < 0) & (poss_diff['Total'] < 0))
h1_win = ((poss_diff['Points'] > 0) & (poss_diff['H1'] > 0) | (poss_diff['Points'] < 0) & (poss_diff['H1'] < 0))
h2_win = ((poss_diff['Points'] > 0) & (poss_diff['H2'] > 0) | (poss_diff['Points'] < 0) & (poss_diff['H2'] < 0))
print len(poss_diff[total_win])
print len(poss_diff[h1_win])
print len(poss_diff[h2_win])

Is there a more Pythonic way to calculate and store the results using lambda, applymap, or another function?

\$\endgroup\$
2
\$\begingroup\$
  • condition a > 0 and b > 0 or a < 0 and b < 0 often can be write as a * b > 0 (it depends on multiply cost)
  • maybe you can use it something as total_win = ( poss_diff.Points * poss_diff.Total > 0 )
  • len(poss_diff[total_win]) can be translate to total_win.sum()
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.