Coder Social home page Coder Social logo

basic returns. about nfldb HOT 21 CLOSED

burntsushi avatar burntsushi commented on July 28, 2024
basic returns.

from nfldb.

Comments (21)

roodhouse avatar roodhouse commented on July 28, 2024

I did this

q = nfldb.Query(db)
q.player(full_name='J.J. Watt')
for p in q.as_aggregate():
    print p 

and got this in return

{'defense_sk_yds': -315L, 'defense_tkl_primary': 19L, 'defense_int_tds': 1L, 'defense_frec_yds': 9L, 'defense_frec': 6L, 'defense_tkl_loss': 82L, 'defense_fgblk': 2L, 'defense_sk': 41.5, 'defense_pass_def': 32L, 'defense_tkl': 202L, 'defense_int': 1L, 'defense_int_yds': 29L, 'defense_qbhit': 112L, 'defense_ffum': 8L, 'defense_ast': 40L, 'defense_tkl_loss_yds': 259L}
[Finished in 1.2s]

I am assuming those are all of JJ Watt's stats for 2013?
What does the "L" mean behind each number?
Is there a way to return the info in a different format?

Like this:
'defense_sk_yds': -315L,
'defense_tkl_primary': 19L

Instead of this:
defense_sk_yds': -315L, 'defense_tkl_primary': 19L

from nfldb.

BurntSushi avatar BurntSushi commented on July 28, 2024

@roodhouse Whenever you write a query, if you don't specify q.game(season_year=2013, season_type='Regular'), then you're going to get results for all data in the database. That includes preseason and postseason.

For example, if you look at the stats you showed, it says J.J. Watt has 41.5 sacks. That should be a signal to you that the data isn't just for one season.

To restrict his stats to the 2013 regular season:

import nfldb

db = nfldb.connect()
q = nfldb.Query(db)

q.game(season_year=2013, season_type='Regular')
q.player(full_name='J.J. Watt')

for pp in q.as_aggregate():
    print pp

Changing the format of the output data isn't really nfldb related. You just have to learn Python. :-) For example:

for pp in q.as_aggregate():
    for field in pp.fields:
        print '%s: %s' % (field, getattr(pp, field))

Would show

[andrew@Liger nfldb] python2 35.py                                                                                                                                                              
defense_sk_yds: -74                                                                                                                                                                                  
defense_frec: 2                                                                                                                                                                                      
defense_fgblk: 2                                                                                                                                                                                     
defense_sk: 10.5                                                                                                                                                                                     
defense_pass_def: 7                                                                                                                                                                                  
defense_tkl: 65                                                                                                                                                                                      
defense_tkl_loss: 22                                                                                                                                                                                 
defense_qbhit: 46                                                                                                                                                                                    
defense_ffum: 4                                                                                                                                                                                      
defense_ast: 15                                                                                                                                                                                      
defense_tkl_loss_yds: 83

And if you wanted to sort the fields, then you could use the sorted function:

for pp in q.as_aggregate():
    for field in sorted(pp.fields):
        print '%s: %s' % (field, getattr(pp, field))

If you want to repeat this pattern over and over for a lot of players, then you should write a function. Here's an example:

import nfldb

db = nfldb.connect()

def season_player_stats(full_name, season):
    q = nfldb.Query(db)

    q.game(season_year=season, season_type='Regular')
    q.player(full_name=full_name)
    pps = q.as_aggregate()

    assert len(pps) < 2, "Make sure only one player was aggregated"
    if len(pps) == 0:  # No players found to aggregate
        return None
    return pps[0]

stats = season_player_stats('J.J. Watt', 2013)
for field in sorted(stats.fields):
    print '%s: %s' % (field, getattr(stats, field))

You can even write a function that will automatically print the statistics for you:

import nfldb

db = nfldb.connect()


def season_player_stats(full_name, season):
    q = nfldb.Query(db)

    q.game(season_year=season, season_type='Regular')
    q.player(full_name=full_name)
    pps = q.as_aggregate()

    assert len(pps) < 2, "Make sure only one player was aggregated"
    if len(pps) == 0:  # No players found to aggregate
        return None
    return pps[0]


def print_nice_stats(stats):
    for field in sorted(stats.fields):
        print '%s: %s' % (field, getattr(stats, field))

for name in ['J.J. Watt', 'Tom Brady', 'Peyton Manning']:
    print(name)
    print_nice_stats(season_player_stats(name, 2013))
    print '------------'

And this outputs:

[andrew@Liger nfldb] python2 35.py                                                                                                                                                              
J.J. Watt
defense_ast: 15
defense_ffum: 4
defense_fgblk: 2
defense_frec: 2
defense_pass_def: 7
defense_qbhit: 46
defense_sk: 10.5
defense_sk_yds: -74
defense_tkl: 65
defense_tkl_loss: 22
defense_tkl_loss_yds: 83
------------
Tom Brady
fumbles_forced: 6
fumbles_lost: 3
fumbles_notforced: 3
fumbles_rec: 5
fumbles_rec_yds: -10
fumbles_tot: 9
offense_tds: 25
offense_yds: 4389
passing_att: 626
passing_cmp: 379
passing_cmp_air_yds: 2368
passing_incmp: 247
passing_incmp_air_yds: 2967
passing_int: 11
passing_sk: 39
passing_sk_yds: -256
passing_tds: 25
passing_twopta: 2
passing_twoptm: 2
passing_yds: 4338
points: 154
punting_tot: 1
punting_yds: 32
rushing_att: 33
rushing_yds: 18
------------
Peyton Manning
defense_tkl: 2
fumbles_forced: 4
fumbles_lost: 6
fumbles_notforced: 6
fumbles_oob: 1
fumbles_rec: 2
fumbles_rec_yds: -4
fumbles_tot: 11
offense_tds: 56
offense_yds: 5497
passing_att: 659
passing_cmp: 450
passing_cmp_air_yds: 2894
passing_incmp: 209
passing_incmp_air_yds: 2179
passing_int: 10
passing_sk: 18
passing_sk_yds: -120
passing_tds: 55
passing_twopta: 1
passing_twoptmissed: 1
passing_yds: 5477
points: 336
rushing_att: 32
rushing_tds: 1
rushing_yds: -31
------------

from nfldb.

BurntSushi avatar BurntSushi commented on July 28, 2024

If you're following via email, then my previous comment contains incorrect code. I've fixed it, but you'll need to come see it on github.com because I don't think edits to comments cause an email re-send.

from nfldb.

roodhouse avatar roodhouse commented on July 28, 2024

I see. Thank you. I am going to look into learning some Python. I've been using treehouse for web development and I think they have a Python course now.

from nfldb.

roodhouse avatar roodhouse commented on July 28, 2024

@BurntSushi Using the function you gave above, if I want to return each stat category for each player I query, even if that player has a 0 value for that stat, can I do that?

Result something like this:

Aaron Rodgers
fumbles_notforced: 0
offense_tds: 3
passing_int: 0
passing_sk: 3

from nfldb.

BurntSushi avatar BurntSushi commented on July 28, 2024

Yes, although there are many of them:

import nfldb

db = nfldb.connect()

player_fields = []
for field, cat in nfldb.stat_categories.iteritems():
    if cat.category_type is nfldb.Enums.category_scope.player:
        player_fields.append(field)
player_fields.sort()


def season_player_stats(full_name, season):
    q = nfldb.Query(db)

    q.game(season_year=season, season_type='Regular')
    q.player(full_name=full_name)
    pps = q.as_aggregate()

    assert len(pps) < 2, "Make sure only one player was aggregated"
    if len(pps) == 0:  # No players found to aggregate
        return None
    return pps[0]


def print_nice_stats(stats):
    for field in player_fields:
        print '%s: %s' % (field, getattr(stats, field))

for name in ['J.J. Watt', 'Tom Brady', 'Peyton Manning']:
    print(name)
    print_nice_stats(season_player_stats(name, 2013))
    print '------------'

This is using the official list of available statistical categories and just enumerating all of them on the pp (play_player) object.

If you only want to show a subset of related fields, then you'll have to create those sets yourself and use them instead. e.g.,

passing_fields = ['passing_tds', 'passing_yds', 'passing_sk', 'passing_int']
for field in passing_fields:
    print getattr(pp, field)

from nfldb.

roodhouse avatar roodhouse commented on July 28, 2024

Thank you. It looks like it is working.

When you say "If you only want to show a subset of related fields, then you'll have to create those sets yourself and use them instead. e.g.,"

That would be if I wanted to narrow the fields down to return less categories?

from nfldb.

BurntSushi avatar BurntSushi commented on July 28, 2024

That would be if I wanted to narrow the fields down to return less categories?

Yes. For example, the code I gave you will show kickret_yds: 0 for Tom Brady. That's kind of silly, but the middle ground, "show only relevant fields, even if they are 0" is best done by you just specifying which fields are relevant manually. You can see all of the fields here: https://github.com/BurntSushi/nfldb/wiki/Statistical-categories

from nfldb.

roodhouse avatar roodhouse commented on July 28, 2024

@BurntSushi The above is now working for me thank you.

I am trying to export the results to a csv file. I researched a bit and I am trying this:

import nfldb
import csv

db = nfldb.connect()


def season_player_stats(full_name, season):
    q = nfldb.Query(db)

    q.game(season_year=season, season_type='Regular', week=1)
    q.player(full_name=full_name)
    pps = q.as_aggregate()

    assert len(pps) < 2, "Make sure only one player was aggregated"
    if len(pps) == 0:  # No players found to aggregate
        return None
    return pps[0]


def print_nice_stats(stats):
    for field in sorted(stats.fields):
        print '%s: %s' % (field, getattr(stats, field))

for name in ['J.J. Watt', 'Tom Brady', 'Peyton Manning']:
    print(name)
    print_nice_stats(season_player_stats(name, 2013))
    print '------------'


name = [(name, season_player_stats(name, 2013)), (name, season_player_stats), (name, season_player_stats)]

with open('somefile.csv', 'w') as f:
    writer = csv.writer(f, delimiter=',')
    for row in name:
        writer.writerow(row)

print "Done Writing"

Within SublimeText the output looks as it should.

The csv file is created but it looks like this:

 COL A                          COL B
1 Peyton Manning | {'passing_incmp_air_yds': 146L, 'passing_sk': 3L, 'passing_att': 42L, etc.. 
2 Peyton Manning | <function season_player_stats at 0x023B30F0>
3 Peyton Manning | <function season_player_stats at 0x023B30F0>

I am sure there is a ton wrong here. But how can I get it to not export Peyton each time, remove the "L" from his stats & produce the stats for the other players as well?

I know this is more a Python question than an nfldb question. Thank you for your time.

from nfldb.

BurntSushi avatar BurntSushi commented on July 28, 2024

A proper answer to your question requires more thought and detail than I'm capable right now, so I'll try to get to it in the morning. (And yes, it is more of a Python question, but that's OK.)

In the mean time, I will leave you with some pointed questions that may help guide you.

  1. Why is it showing Peyton Manning every time? I see that you have used name in each row without ever changing the value of name. The last time it was set was on the last iteration of the for name in [...]: loop.
  2. A row should have as many columns as there are statistics, but your row only has two columns. You already have the code to loop over each statistical field, now you only need to figure out how to use that to make a row. Consider this code that creates an empty list and appends numbers to it:
numbers = []
numbers.append(1)
numbers.append(2)
numbers.append(3)
numbers.append(4)
numbers.append(5)
print numbers
# Output: [1, 2, 3, 4, 5]

But we know how to loop:

numbers = []
for n in [1, 2, 3, 4, 5]:
    numbers.append(n)
print numbers
# Output: [1, 2, 3, 4, 5]

Try to adapt this use of lists with append to make your row lists. (Each element in the list is a cell and you want a cell for each field in stats.field.

from nfldb.

roodhouse avatar roodhouse commented on July 28, 2024

Thanks again for your time.

"Why is it showing Peyton Manning every time? I see that you have used name in each row without ever changing the value of name. The last time it was set was on the last iteration of the for name in [...]: loop."

I thought if I named the below "name"

name = [(name, season_player_stats(name, 2013)), (name, season_player_stats), (name, season_player_stats)]

That it would reach up to here

for name in ['J.J. Watt', 'Tom Brady', 'Peyton Manning']:

And draw each player name in sequential order. Yet Watt and Brady are skipped and Manning is repeated 2x.

I also that if I called

(name, season_player_stats(name, 2013))

that it would produce what is produced with the function season_player_stats above.

I will toy with this and see if I can get it going, address number 2 and give an update in the morning as well.

from nfldb.

roodhouse avatar roodhouse commented on July 28, 2024

Alright. Still struggling. Although I did produce some different results.

As you can see under "name" I put 3 players and some random numbers. It produced 2 rows of the same data for each player (I understand why, and will fix it).

 name = [('Peyton', 10, 2013), ('Tom', 12, 2013), ('JJ', 0, 2013)]

with open('somefile.csv', 'w') as f:
    writer = csv.writer(f, delimiter=',')
    for row in name:
        writer.writerow(row)
        writer.writerow(row)

print "Done Writing"

What I thought would happen or thought could happen, was if I plugged key words into

name = [(1,2,3), (1,2,3), (1, 2, 3)]

That it would produce the results from higher up in the code.

def season_player_stats(full_name, season):
    q = nfldb.Query(db)

    q.game(season_year=season, season_type='Regular', week=1)
    q.player(full_name=full_name)
    pps = q.as_aggregate()

    assert len(pps) < 2, "Make sure only one player was aggregated"
    if len(pps) == 0:  # No players found to aggregate
        return None
    return pps[0]


def print_nice_stats(stats):
    for field in sorted(stats.fields):
        print '%s: %s' % (field, getattr(stats, field))

for name in ['J.J. Watt', 'Tom Brady', 'Peyton Manning']:
    print(name)
    print_nice_stats(season_player_stats(name, 2013))
    print '------------' 

For instance this

name = [(name, season_player_stats(name, 2013)), (name, season_player_stats(name, 2013)), (name, season_player_stats(name, 2013))]

Would produce a nice csv file with each player in col A and their corresponding stats for each category in cells B1:CR1. I thought that because the higher up code, does produce player name and stats for each category available.

As you know it did not, and what I tinkered with did not either.

Thank you again for any help you can give me. I do play to start a Python course soon, so maybe these questions for you will stop. ;)

from nfldb.

ochawkeye avatar ochawkeye commented on July 28, 2024

Your problem is in this sequence of events right here:

for name in ['J.J. Watt', 'Tom Brady', 'Peyton Manning']:
    print(name)
    print_nice_stats(season_player_stats(name, 2013))
    print '------------'

name = [(name, season_player_stats(name, 2013)), (name, season_player_stats), (name, season_player_stats)]

Let's track what the value of name is...
1st time through for name in [...], name is 'J.J. Watt'. You do some stuff with it and go back through the loop.
2nd time through, for name in[...], name is 'Tom Brady'. Do some stuff with it and go back through the loop.
3rd time through, for name in [...], name is 'Peyton Manning'. Do some stuff with it and we're to the end of the loop so we exit the loop. But name is still 'Peyton Manning'.

Before we get to name = [(name, season_player_stats(name, 2013)), (name, season_player_stats), (name, season_player_stats)] I want to point out one thing about Python that might not be intuitive. You can use the current value of a variable to define a variable with that same name. To put it another way.

INPUT
a = 10
print a
a = a * a * a
print a
========
OUTPUT:
10
1000

Keep that in mind as we read

name = [(name, season_player_stats(name, 2013)), (name, season_player_stats), (name, season_player_stats)]

And the fact that name as it enters that code is still 'Peyton Manning'.

So what you are now saying is:

name = [('Peyton Manning', season_player_stats('Peyton Manning', 2013)), ('Peyton Manning', season_player_stats), ('Peyton Manning', season_player_stats)]

Not really what you wanted.

I think I kind of see what you're trying to do, but I think you will be disappointed with the results because you are trying to write only non-0 stat results to your csv file, but JJ Watts stats don't look anything like Peyton Manning's stats.

The following gives you a legible (though fairly meaningless) .csv file and might help push you in the right direction.

import nfldb
import csv

db = nfldb.connect()


def season_player_stats(full_name, season):
    q = nfldb.Query(db)

    q.game(season_year=season, season_type='Regular', week=1)
    q.player(full_name=full_name)
    pps = q.as_aggregate()

    assert len(pps) < 2, "Make sure only one player was aggregated"
    if len(pps) == 0:  # No players found to aggregate
        return None
    return pps[0]


def print_nice_stats(stats):
    for field in sorted(stats.fields):
        print '%s: %s' % (field, getattr(stats, field))

for name in ['J.J. Watt', 'Tom Brady', 'Peyton Manning']:
    print(name)
    print_nice_stats(season_player_stats(name, 2013))
    print '------------'


#name = [(name, season_player_stats(name, 2013)), (name, season_player_stats), (name, season_player_stats)]

with open('somefile.csv', 'w') as f:
    writer = csv.writer(f, delimiter=',')
    for name in ['J.J. Watt', 'Tom Brady', 'Peyton Manning']:
        x = season_player_stats(name, 2013)
        writer.writerow([name]+[getattr(x, a) for a in x.fields])

print "Done Writing"

But this is all stuff you will pick up as you learn Python.

from nfldb.

BurntSushi avatar BurntSushi commented on July 28, 2024

@ochawkeye 👍

If you wanted to output all fields in your CSV, then you should change this line:

writer.writerow([name]+[getattr(x, a) for a in x.fields])

To this line:

writer.writerow([name]+[getattr(x, a) for a in player_fields])

You'll also need to add this somewhere after import nfldb but toward the top:

player_fields = []
for field, cat in nfldb.stat_categories.iteritems():
    if cat.category_type is nfldb.Enums.category_scope.player:
        player_fields.append(field)
player_fields.sort()

This snippet of code is collecting all of the player statistic categories in nfldb. The line change above is using those fields instead of just the non-zero fields.

from nfldb.

roodhouse avatar roodhouse commented on July 28, 2024

Thanks to both of you. I won't be able to really soak all this in until
later in the weekend or early next week. But I didn't want y'all to think
that I was not grateful.

On Friday, August 1, 2014, Andrew Gallant [email protected] wrote:

@ochawkeye https://github.com/ochawkeye [image: 👍]

If you wanted to output all fields in your CSV, then you should change
this line:

writer.writerow([name]+[getattr(x, a) for a in x.fields])

To this line:

writer.writerow([name]+[getattr(x, a) for a in player_fields])

You'll also need to add this somewhere after import nfldb but toward the
top:

player_fields = []for field, cat in nfldb.stat_categories.iteritems():
if cat.category_type is nfldb.Enums.category_scope.player:
player_fields.append(field)player_fields.sort()

This snippet of code is collecting all of the player statistic categories
in nfldb. The line change above is using those fields instead of just the
non-zero fields.


Reply to this email directly or view it on GitHub
#35 (comment).

from nfldb.

BurntSushi avatar BurntSushi commented on July 28, 2024

@roodhouse 👍

from nfldb.

roodhouse avatar roodhouse commented on July 28, 2024

@BurntSushi @ochawkeye Thanks so much!

I am in the middle of a crash course on python. I feel lost in a fog currently. I take a lesson and the do a "code challenge" and can't figure out what I am being asked to do. Hopefully this is a learning curve thing and nothing more.

Are there any solid resources you can suggest for learning Python? I know about the python website, but I would like something that will walk me through it while I practice it. Not just read about it etc..

from nfldb.

BurntSushi avatar BurntSushi commented on July 28, 2024

The "fog" is definitely part of the learning curve. Understanding the problem is 99% of the battle. :)

I have heard good things about Learn Python the Hard Way. There are lots of hands on examples.

from nfldb.

albertlyu avatar albertlyu commented on July 28, 2024

@roodhouse, the best way I learned Python was by doing something I love. Pick a sports website and scrape data with BeautifulSoup4. Or take nfldb and write Python queries against the database. I know a lot of people use tutorials and crash courses, but I think a lot about the learning curve of a new scripting language is learning how to find information accurately and fast. And I've found that I've had a greater motivation to do so when the application or problem I am trying to solve is related to sports.

Otherwise, I learned a lot from www.pythonchallenge.com. A bit antiquated, sure, but there are a lot of fun puzzles in there. Google Code Jam has a lot of puzzles as well, with tons of practice problems from previous competitions. https://code.google.com/codejam/contests.html You can complete the problems in any language you want, but I reckon that most use Python.

from nfldb.

ochawkeye avatar ochawkeye commented on July 28, 2024

@roodhouse What got me really interested in Python was Udacity's CS101 course. It was the first time I was able to see all of the pieces I had been playing with at Codecadamy come together in a full project. I finished that course only days before stumbling upon nflgame.

I see people recommend Project Euler a lot. I think I completed nearly 100 of the challenges, but it really wasn't for me - it's just so focused on finding optimized mathematical algorithms...I just never felt I could properly apply it to anything I was actually interested in. I might recommend Code Abbey for some challenges that aren't so math-centric.

from nfldb.

BurntSushi avatar BurntSushi commented on July 28, 2024

@roodhouse In the interest of keeping the tracker clean, I'm going to close this. If you need more help or have more questions, please don't hesitate to open another issue. (In fact, opening a new issue for each new question is preferred.)

from nfldb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.