BurntSushi / nflgame

An API to retrieve and read NFL Game Center JSON data. It can work with real-time data, which can be used for fantasy football.
http://pdoc.burntsushi.net/nflgame
The Unlicense
1.27k stars 413 forks source link

kicking_fgm_yds Question #211

Open cphipps opened 8 years ago

cphipps commented 8 years ago

I am trying to write a function that can look at Field Goals made by kickers to determine how many they made 50+ yards, 40-49 yards, and 0-39 yards. You could search this for a specific week or entire season. The exact field position of the kick does not matter just if it falls in that range.

I see there is kicking_fgm_yds but that totals up total yards of all kicks. Is there a way to extract how long each kick made was instead of the total for all kicks for the game?

Thanks Corey

ochawkeye commented 8 years ago

Sounds like you're trying to aggregate plays? If you want individual play kicking_fgm_yds then you have to deal with individual plays.

For example, here are the plays where a FG was made that was less than 40 yards in week 1 of 2015.

import nflgame
games = nflgame.games(2015, week=1)
plays = nflgame.combine_plays(games)
for play in plays.filter(kicking_fgm=True, kicking_fgm_yds__lt=40):
    print play.players.kicking(), play.kicking_fgm_yds
[J.Scobee] 24
[R.Gould] 28
[M.Crosby] 37
[G.Zuerlein] 33
[S.Hauschka] 25
[S.Hauschka] 27
[S.Hauschka] 35
[G.Zuerlein] 37
[J.Myers] 22
[A.Franks] 22
[T.Coons] 26
[N.Folk] 34
[C.Santos] 27
[R.Bullock] 34
[Z.Hocker] 37
[Z.Hocker] 23
[Z.Hocker] 33
[J.Lambo] 32
[J.Lambo] 25
[B.McManus] 33
[M.Nugent] 32
[M.Nugent] 35
[D.Bailey] 21
[D.Bailey] 32
[J.Brown] 30
[J.Brown] 19
[C.Parkey] 29
[M.Bryant] 39
[P.Dawson] 30
[B.Walsh] 37
[P.Dawson] 25
ochawkeye commented 8 years ago

Have a look at https://github.com/BurntSushi/nflgame/issues/174, https://github.com/BurntSushi/nflgame/issues/116, and https://github.com/BurntSushi/nflgame/issues/26 for some more samples.

cphipps commented 8 years ago

Thanks. I have been using player.full_name but it does not look like that is available under plays. Are you aware of a built in way to do that?

AttributeError: 'GenPlayerStats' object has no attribute 'full_name'

ochawkeye commented 8 years ago
import nflgame
games = nflgame.games(year=2015, week=1)
plays = nflgame.combine_plays(games)
for play in plays.filter(kicking_fgm=True, kicking_fgm_yds__lt=40):
    for p in play.players:
        print p.player.full_name, play.kicking_fgm, play.kicking_fgm_yds
Josh Scobee 1 24
Robbie Gould 1 28
Mason Crosby 1 37
Greg Zuerlein 1 33
Steven Hauschka 1 25
Steven Hauschka 1 27
Steven Hauschka 1 35
Greg Zuerlein 1 37
Jason Myers 1 22
Andrew Franks 1 22
Travis Coons 1 26
Nick Folk 1 34
Cairo Santos 1 27
Randy Bullock 1 34
Zach Hocker 1 37
Zach Hocker 1 23
Zach Hocker 1 33
Josh Lambo 1 32
Josh Lambo 1 25
Brandon McManus 1 33
Mike Nugent 1 32
Mike Nugent 1 35
Dan Bailey 1 21
Dan Bailey 1 32
Josh Brown 1 30
Josh Brown 1 19
Cody Parkey 1 29
Matt Bryant 1 39
Phil Dawson 1 30
Blair Walsh 1 37
Phil Dawson 1 25
cphipps commented 8 years ago

I am trying to figure out a way using dictionaries and/or lists to have the output above ochawkeye created to only list a kicker one time and then each of his made field goal made after his name. For example above Steve Hauschka made a 25 27 and 35 but it has a separate line for each one.

Output would look something like:

SEA Steve Hauschka 25 27 35

From there I could calculate how many points per fg the player gets based on distance of the FG. I could possibly run a if statement to assign a value to each index based on the range the FG was made in.

I am relatively new to Python so forgive me if I am going about this the wrong way.

Thanks

ochawkeye commented 8 years ago

This output is in the format you requested I believe.

import nflgame
from collections import defaultdict
games = nflgame.games(year=2015, week=1)
plays = nflgame.combine_plays(games)

cphipps_fgs = defaultdict(list)
for play in plays.filter(kicking_fgm=True, kicking_fgm_yds__lt=40):
    for p in play.players:
        cphipps_fgs[p.player].append(play.kicking_fgm_yds)
        #print p.player.full_name, play.kicking_fgm, play.kicking_fgm_yds
for kicker in cphipps_fgs:
    print kicker.team, kicker.full_name, ' '.join(str(fg) for fg in cphipps_fgs[kicker])
CIN Mike Nugent 32 35
CHI Robbie Gould 28
GB Mason Crosby 37
MIN Blair Walsh 37
DAL Dan Bailey 21 32
JAC Jason Myers 22
 Greg Zuerlein 33 37
NYG Josh Brown 30 19
ATL Matt Bryant 39
PHI Cody Parkey 29
 Josh Scobee 24
DEN Brandon McManus 33
CIN Zach Hocker 37 23 33
KC Cairo Santos 27
SD Josh Lambo 32 25
CLE Travis Coons 26
SF Phil Dawson 30 25
MIA Andrew Franks 22
NYJ Nick Folk 34
 Randy Bullock 34
SEA Steven Hauschka 25 27 35
cphipps commented 8 years ago

Thanks much. I altered it a bit to replace the yards with the amount of points the player would get (0-39 = 3pts, 40-49 = 4pts, and 50+ = 5. I am trying to get total XP's, added to the same player line as well and then sum it with FG points to determine the total fantasy points per Kicker. Can the kicking_xpmade be used here as well? I started playing with it but could not get it working.

Also, just curious as to why the team is not listed next to some of the kickers?


import nflgame

from collections import defaultdict games = nflgame.games(year=2015, week=1) plays = nflgame.combine_plays(games)

total_fgs = defaultdict(list) for play in plays.filter(kicking_fgm=True): for p in play.players: if play.kicking_fgm_yds >= 50: fg_50_plus = 5 total_fgs[p.player].append(fg_50_plus) elif play.kicking_fgm_yds >= 40: fg_40_49 = 4 total_fgs[p.player].append(fg_40_49) else: fg_0_39 = 3 total_fgs[p.player].append(fg_0_39)

for kicker in total_fgs: total_fd_fg_pts = sum(total_fgs[kicker]) print kicker.team.ljust(3), kicker.full_name.ljust(20), ' '.join(str(fg) for fg in total_fgs[kicker]).ljust(10), "Total FG Points: " + str(total_fd_fg_pts)


OUTPUT

NYJ Nick Folk 3 Total FG Points: 3 Greg Zuerlein 3 3 Total FG Points: 6 ARI Chandler Catanzaro 4 Total FG Points: 4 MIA Andrew Franks 3 Total FG Points: 3 KC Cairo Santos 3 4 Total FG Points: 7 BUF Dan Carpenter 4 4 Total FG Points: 8 GB Mason Crosby 3 Total FG Points: 3 ATL Matt Bryant 4 3 4 4 Total FG Points: 15 SEA Steven Hauschka 3 3 3 Total FG Points: 9 CHI Robbie Gould 3 5 4 Total FG Points: 12 CIN Mike Nugent 3 3 Total FG Points: 6 SD Josh Lambo 3 3 Total FG Points: 6 NO Kai Forbath 4 Total FG Points: 4 PHI Cody Parkey 3 Total FG Points: 3 NYG Josh Brown 5 4 3 3 Total FG Points: 15 CAR Graham Gano 4 4 Total FG Points: 8 CLE Travis Coons 3 Total FG Points: 3 DAL Dan Bailey 3 3 Total FG Points: 6 Randy Bullock 3 4 Total FG Points: 7 BAL Justin Tucker 5 4 Total FG Points: 9 CIN Zach Hocker 3 3 4 3 Total FG Points: 13 Josh Scobee 4 3 Total FG Points: 7 Jason Myers 3 Total FG Points: 3 DEN Brandon McManus 5 5 4 3 Total FG Points: 17 MIN Blair Walsh 3 Total FG Points: 3 SF Phil Dawson 3 3 Total FG Points: 6

ochawkeye commented 8 years ago

I am trying to get total XP's, added to the same player line as well and then sum it with FG points to determine the total fantasy points per Kicker.

When you run for play in plays.filter(kicking_fgm=True): you're throwing out every play that does not result in a made field goal. That includes plays that result in a made extra point since kicking_xpmade is not the same thing as a kicking_fgm.

If you were looping over these plays.filter(kicking_xpmade=True), you might have been running into the fact that the plays you were looking at were exhausted once you iterated over them. (Mentioned in https://github.com/BurntSushi/nflgame/issues/26). You just need to generate those plays again so you can filter on them a second time.

import nflgame
from collections import defaultdict

games = nflgame.games(year=2015, week=1)
total_fgs = defaultdict(list)

plays = nflgame.combine_plays(games)
for play in plays.filter(kicking_xpmade=True):
    for p in play.players:
        total_fgs[p.player].append(1)

plays = nflgame.combine_plays(games)
for play in plays.filter(kicking_fgm=True):
    for p in play.players:
       #  total_fgs[p.player].append(max(min(5, play.kicking_fgm_yds/10), 3))  # Look how cute that is :P...
        if play.kicking_fgm_yds >= 50:
            total_fgs[p.player].append(5)
        elif play.kicking_fgm_yds >= 40:
            total_fgs[p.player].append(4)
        else:
            total_fgs[p.player].append(3)

for kicker in total_fgs:
    total_fd_fg_pts = sum(total_fgs[kicker])
    print kicker.team.ljust(3), kicker.full_name.ljust(20), ' '.join(str(fg) for fg in total_fgs[kicker]).ljust(10), "Total FG Points: " + str(total_fd_fg_pts)

Also, just curious as to why the team is not listed next to some of the kickers?

Those guys aren't currently on an active roster. Keep in mind that the player meta data reflects their status today. You can most clearly see this if you look at a guy that has been around for awhile on different teams. Connor Barth played for Tampa Bay in 2012. Running similar to above:

games = nflgame.games(year=2012, home='TB', away='TB')
plays = nflgame.combine_plays(games)
broken_fgs = defaultdict(list)
for play in plays.filter(kicking_xpmade=True):
    for p in play.players:
        broken_fgs[p.player].append(1)
for kicker in broken_fgs:
    total_fd_fg_pts = sum(broken_fgs[kicker])
    print kicker.team.ljust(3), kicker.full_name.ljust(20), ' '.join(str(fg) for fg in broken_fgs[kicker]).ljust(10), "Total FG Points: " + str(total_fd_fg_pts)
DAL Dan Bailey           1          Total FG Points: 1
HOU Nick Novak           1 1 1      Total FG Points: 3
DET Matt Prater          1 1 1      Total FG Points: 3
OAK Sebastian Janikowski 1 1 1      Total FG Points: 3
    Lawrence Tynes       1 1 1      Total FG Points: 3
MIN Blair Walsh          1 1        Total FG Points: 2
    Billy Cundiff        1 1 1      Total FG Points: 3
    Justin Medlock       1 1 1      Total FG Points: 3
ATL Matt Bryant          1 1 1 1 1  Total FG Points: 5
    Garrett Hartley      1 1 1 1 1 1 1 1 1 1 Total FG Points: 10
    Alex Henery          1 1        Total FG Points: 2
    Greg Zuerlein        1 1 1      Total FG Points: 3
NO  Connor Barth         1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Total FG Points: 35

Says there NO because that is who he currently plays for.

cphipps commented 8 years ago

First off thanks much OC for all your help so far it is much appreciated. It is working now but the piece I am trying to after total points is the Home Team, Home Score, Away Team, Away Score. I have something in place but for each player it is only putting the NYJ & IND game which is probably the first game on the schedule. I imagine I have to some how tie the players team to the home or away team so the correct score is associated with each line. Would you do if statements to see if kicking.team match home or away and if print out that score? Below is the code I am referring to and the current output.

for kicker in total_fgs: total_fd_fg_pts = sum(total_fgs[kicker]) no_team = "n/a" join_kicks = ' '.join(str(fg) for fg in total_fgs[kicker]) for game in games: home_team = game.home home_score = game.score_home away_team = game.away away_score = game.score_away if kicker.team is "": print no_team, kicker.full_name.ljust(20), join_kicks.ljust(20), "FD Points: " + str(total_fd_fg_pts).ljust(3), \ "Away: " + str(away_team).ljust(3), "Score: " + str(away_score).ljust(3), "Home: " + str(home_team).ljust(3), "Score: " + str(home_score).ljust(3) csv_file.write(str(no_team) + ',' + str(kicker.full_name) + ',' + str(join_kicks) + ',' + str(total_fd_fg_pts) + '\n') else: print kicker.team.ljust(3), kicker.full_name.ljust(20), join_kicks.ljust(20), "FD Points: " + str(total_fd_fg_pts).ljust(3), \ "Away: " + str(away_team).ljust(3), "Score: " + str(away_score).ljust(3), "Home: " + str(home_team).ljust(3), "Score: " + str(home_score).ljust(3) csv_file.write(str(kicker.team) + ',' + str(kicker.full_name) + ',' + str(join_kicks) + ',' + str(total_fd_fg_pts) + '\n') csv_file.close()


OUTPUT

DET Matt Prater 1 3 FD Points: 4 Away: NYJ Score: 20 Home: IND Score: 7
BAL Justin Tucker 1 1 1 3 3 3 3 FD Points: 15 Away: NYJ Score: 20 Home: IND Score: 7
CIN Zach Hocker 1 FD Points: 1 Away: NYJ Score: 20 Home: IND Score: 7
n/a Josh Scobee 1 1 1 FD Points: 3 Away: NYJ Score: 20 Home: IND Score: 7
CHI Will Sutton 1 FD Points: 1 Away: NYJ Score: 20 Home: IND Score: 7
n/a Jason Myers 1 1 3 5 3 FD Points: 13 Away: NYJ Score: 20 Home: IND Score: 7
DEN Brandon McManus 1 1 1 5 FD Points: 8 Away: NYJ Score: 20 Home: IND Score: 7
MIN Blair Walsh 1 1 3 3 FD Points: 8 Away: NYJ Score: 20 Home: IND Score: 7
SF Phil Dawson 1 4 FD Points: 5 Away: NYJ Score: 20 Home: IND Score: 7
NE Stephen Gostkowski 1 1 1 1 4 3 5 3 FD Points: 19 Away: NYJ Score: 20 Home: IND Score: 7
ARI Chandler Catanzaro 1 1 1 1 1 FD Points: 5 Away: NYJ Score: 20 Home: IND Score: 7
n/a Kyle Brindza 1 1 5 3 3 3 FD Points: 16 Away: NYJ Score: 20 Home: IND Score: 7
NYJ Nick Folk 1 1 3 4 FD Points: 9 Away: NYJ Score: 20 Home: IND Score: 7
WAS Dustin Hopkins 1 1 1 4 FD Points: 7 Away: NYJ Score: 20 Home: IND Score: 7
TEN Ryan Succop 1 1 FD Points: 2 Away: NYJ Score: 20 Home: IND Score: 7
KC Cairo Santos 1 1 3 FD Points: 5 Away: NYJ Score: 20 Home: IND Score: 7
WAS David Bruton 1 FD Points: 1 Away: NYJ Score: 20 Home: IND Score: 7
MIA Andrew Franks 1 1 4 4 FD Points: 10 Away: NYJ Score: 20 Home: IND Score: 7
BUF Dan Carpenter 1 1 FD Points: 2 Away: NYJ Score: 20 Home: IND Score: 7
GB Mason Crosby 1 5 3 4 3 FD Points: 16 Away: NYJ Score: 20 Home: IND Score: 7
n/a Greg Zuerlein 1 5 FD Points: 6 Away: NYJ Score: 20 Home: IND Score: 7
ATL Matt Bryant 1 1 1 4 FD Points: 7 Away: NYJ Score: 20 Home: IND Score: 7
SEA Steven Hauschka 1 1 5 FD Points: 7 Away: NYJ Score: 20 Home: IND Score: 7
IND Adam Vinatieri 1 FD Points: 1 Away: NYJ Score: 20 Home: IND Score: 7
BUF Aaron Williams 1 FD Points: 1 Away: NYJ Score: 20 Home: IND Score: 7
n/a Quinton Coples 1 FD Points: 1 Away: NYJ Score: 20 Home: IND Score: 7
CHI Robbie Gould 1 1 4 3 5 FD Points: 14 Away: NYJ Score: 20 Home: IND Score: 7
CIN Mike Nugent 1 1 1 3 FD Points: 6 Away: NYJ Score: 20 Home: IND Score: 7
SD Josh Lambo 1 4 3 FD Points: 8 Away: NYJ Score: 20 Home: IND Score: 7
n/a Randy Bullock 1 1 4 FD Points: 6 Away: NYJ Score: 20 Home: IND Score: 7
OAK Sebastian Janikowski 1 1 1 1 3 4 4 FD Points: 15 Away: NYJ Score: 20 Home: IND Score: 7
PHI Cody Parkey 1 4 FD Points: 5 Away: NYJ Score: 20 Home: IND Score: 7
NYG Josh Brown 1 1 3 4 FD Points: 9 Away: NYJ Score: 20 Home: IND Score: 7
CAR Graham Gano 1 1 1 3 FD Points: 6 Away: NYJ Score: 20 Home: IND Score: 7
OAK Dan Williams 3 FD Points: 3 Away: NYJ Score: 20 Home: IND Score: 7
CLE Travis Coons 1 1 1 FD Points: 3 Away: NYJ Score: 20 Home: IND Score: 7
DAL Dan Bailey 1 3 3 FD Points: 7 Away: NYJ Score: 20 Home: IND Score: 7