Python contributing example

nflfastR python

Showing how to contribute using Python code

Ben Baldwin https://twitter.com/benbbaldwin
08-20-2020

Contributing in python

There is some stuff to install and setup:

Once setup is done:

Start working on your document. You can put in python code chunks as in the examples below (see here for the source code).

Once everything is set up, you can just use python code as normal. Here is a quick example borrowing some code from from Deryck’s nflfastR python guide.

import pandas
data = pandas.read_csv("https://github.com/guga31bb/nflfastR-data/blob/master/data/play_by_play_2019.csv.gz?raw=True", compression='gzip', low_memory=False)
data = data.loc[(data.play_type.isin(['no_play','pass','run'])) & (data.epa.isna()==False)]
data.groupby('posteam')[['epa']].mean()
              epa
posteam          
ARI      0.002421
ATL      0.009436
BAL      0.165036
BUF     -0.030460
CAR     -0.091361
CHI     -0.082597
CIN     -0.095607
CLE     -0.039001
DAL      0.104154
DEN     -0.057336
DET     -0.046365
GB       0.054262
HOU      0.026844
IND      0.002937
JAX     -0.060641
KC       0.167632
LA      -0.007031
LAC      0.027416
LV       0.024514
MIA     -0.082475
MIN      0.024179
NE      -0.004101
NO       0.060163
NYG     -0.062409
NYJ     -0.147206
PHI     -0.002494
PIT     -0.146697
SEA      0.024912
SF       0.066445
TB      -0.017379
TEN      0.058257
WAS     -0.133023

Grouping by QBs:

qbs = data.groupby(['passer','posteam'], as_index=False).agg({'epa':'mean',
                                                              'cpoe':'mean',
                                                              'play_id':'count'})
# at least 200 plays
qbs = qbs.loc[qbs.play_id>199]
# sort by EPA
qbs.sort_values('epa', ascending=False, inplace=True)

#Round to two decimal places where appropriate
qbs = qbs.round(2)

#Rename columns
qbs.columns = ['Player','Team','EPA per Dropback','CPOE','Dropbacks']

qbs
            Player Team  EPA per Dropback   CPOE  Dropbacks
86       P.Mahomes   KC              0.32   2.26        721
71       L.Jackson  BAL              0.29   2.76        554
81      M.Stafford  DET              0.22   2.09        330
39      D.Prescott  DAL              0.19   0.97        675
95     R.Tannehill  TEN              0.19   6.36        421
28         D.Brees   NO              0.18   6.20        447
52     J.Garoppolo   SF              0.17   1.81        629
96        R.Wilson  SEA              0.16   7.10        732
64       K.Cousins  MIN              0.16   3.77        585
40        D.Watson  HOU              0.15   2.12        715
6        A.Rodgers   GB              0.15   1.72        755
29          D.Carr   LV              0.14   5.69        574
87        P.Rivers  LAC              0.11   3.72        676
105  T.Bridgewater   NO              0.09   0.41        235
53          J.Goff   LA              0.09  -1.79        703
61       J.Winston   TB              0.08   0.67        743
92   R.Fitzpatrick  MIA              0.07  -0.48        612
25         C.Wentz  PHI              0.07  -0.77        719
79          M.Ryan  ATL              0.06   1.80        731
104        T.Brady   NE              0.06  -2.53        724
46      J.Brissett  IND              0.06  -3.26        540
45         J.Allen  BUF              0.03  -2.29        618
14      B.Mayfield  CLE              0.02  -2.96        641
67        K.Murray  ARI              0.02  -1.52        654
18        C.Keenum  WAS              0.01  -1.09        285
44    G.Minshew II  JAX              0.01  -4.28        592
75       M.Mariota  TEN              0.01  -3.21        209
1         A.Dalton  CIN             -0.00  -3.27        601
97       S.Darnold  NYJ             -0.01   1.04        516
62         K.Allen  CAR             -0.03  -0.95        574
36         D.Jones  NYG             -0.03  -1.94        560
82      M.Trubisky  CHI             -0.03  -2.33        605
78       M.Rudolph  PIT             -0.03  -1.23        331
51        J.Flacco  DEN             -0.06  -0.21        313
32       D.Haskins  WAS             -0.18  -4.65        255
27        D.Blough  DET             -0.20 -13.59        209

Hopefully the process is painless once all the setup is done.

View source code on GitHub

Corrections

If you see mistakes or want to suggest changes, please create an issue on the source repository.

Reuse

Text and figures are licensed under Creative Commons Attribution CC BY-NC 4.0. Source code is available at https://github.com/mrcaseb/open-source-football, unless otherwise noted. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".

Citation

For attribution, please cite this work as

Baldwin (2020, Aug. 20). Open Source Football: Python contributing example. Retrieved from https://www.opensourcefootball.com/posts/2020-08-20-python-contributing-example/

BibTeX citation

@misc{baldwin2020python,
  author = {Baldwin, Ben},
  title = {Open Source Football: Python contributing example},
  url = {https://www.opensourcefootball.com/posts/2020-08-20-python-contributing-example/},
  year = {2020}
}