Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Average Points Per Game #13

Closed
claybuster opened this issue Dec 1, 2013 · 1 comment
Closed

Average Points Per Game #13

claybuster opened this issue Dec 1, 2013 · 1 comment

Comments

@claybuster
Copy link

How would I be able to figure out the average points per game? I can't figure out how to add up all scores from a team's games for the current season. I tried to contact you on IRC for a couple days and still hadn't heard back.

@BurntSushi
Copy link
Owner

I know this was answered for you already using nflgame, but here's the code to do it with nfldb. Note that this outputs the list twice; once sorted by team name and again sorted by the average in descending order. This is done for all finished games this season.

from collections import defaultdict

import nfldb

def mean(xs):
    return float(sum(xs)) / float(len(xs))

db = nfldb.connect()
team_scores = defaultdict(list)

q = nfldb.Query(db)
q.game(season_year=2013, season_type='Regular', finished=True)
for game in q.as_games():
    team_scores[game.home_team].append(game.home_score)
    team_scores[game.away_team].append(game.away_score)

avgs = map(lambda team: (team, mean(team_scores[team])), team_scores)

# Sorted by team name.
for team, avg in sorted(avgs, key=lambda (x1, x2): x1):
    print '%s, avg ppg: %f' % (team, avg)

print '\n'

# Sorted by ppg average, descending.
for team, avg in sorted(avgs, key=lambda (x1, x2): x2, reverse=True):
    print '%s, avg ppg: %f' % (team, avg)

The output should be:

[andrew@Liger nflgame] python2 avg-points.py 
ARI, avg ppg: 22.916667
ATL, avg ppg: 21.750000
BAL, avg ppg: 20.500000
BUF, avg ppg: 22.250000
CAR, avg ppg: 23.750000
CHI, avg ppg: 26.916667
CIN, avg ppg: 24.333333
CLE, avg ppg: 19.250000
DAL, avg ppg: 27.416667
DEN, avg ppg: 38.666667
DET, avg ppg: 23.833333
GB, avg ppg: 23.666667
HOU, avg ppg: 19.166667
IND, avg ppg: 23.750000
JAC, avg ppg: 14.500000
KC, avg ppg: 24.833333
MIA, avg ppg: 21.000000
MIN, avg ppg: 24.083333
NE, avg ppg: 26.833333
NO, avg ppg: 26.000000
NYG, avg ppg: 19.750000
NYJ, avg ppg: 15.750000
OAK, avg ppg: 19.750000
PHI, avg ppg: 25.000000
PIT, avg ppg: 21.916667
SD, avg ppg: 23.250000
SEA, avg ppg: 28.333333
SF, avg ppg: 24.750000
STL, avg ppg: 23.250000
TB, avg ppg: 18.083333
TEN, avg ppg: 22.000000
WAS, avg ppg: 22.416667


DEN, avg ppg: 38.666667
SEA, avg ppg: 28.333333
DAL, avg ppg: 27.416667
CHI, avg ppg: 26.916667
NE, avg ppg: 26.833333
NO, avg ppg: 26.000000
PHI, avg ppg: 25.000000
KC, avg ppg: 24.833333
SF, avg ppg: 24.750000
CIN, avg ppg: 24.333333
MIN, avg ppg: 24.083333
DET, avg ppg: 23.833333
CAR, avg ppg: 23.750000
IND, avg ppg: 23.750000
GB, avg ppg: 23.666667
STL, avg ppg: 23.250000
SD, avg ppg: 23.250000
ARI, avg ppg: 22.916667
WAS, avg ppg: 22.416667
BUF, avg ppg: 22.250000
TEN, avg ppg: 22.000000
PIT, avg ppg: 21.916667
ATL, avg ppg: 21.750000
MIA, avg ppg: 21.000000
BAL, avg ppg: 20.500000
NYG, avg ppg: 19.750000
OAK, avg ppg: 19.750000
CLE, avg ppg: 19.250000
HOU, avg ppg: 19.166667
TB, avg ppg: 18.083333
NYJ, avg ppg: 15.750000
JAC, avg ppg: 14.500000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants