Fantasy Football League History

This is post number 2 in the league history series. This time around we will expand our league history analysis. In this part of the intermediate series we'll be diving into researching and analyzing fantasy football league history data. If you're like me, being in a competitive fantasy league for nearly a decade means the competition is fierce. Our league does our best remembering what happened in years past to retain bragging rights, but there must be a better way. This post will go into how we can use the ESPN API to pull league history data (teams, years, scores, matchups) to create a record of your league.

It turns out, ESPN has an API with everything recorded assuming you stuck with the same league and reactivated it each year. Each team has a unique id tied to it that we can use pull individual matchups, scores, players, etc. I will show a few examples of what we can do with this data and how we can explore it, but the applications are endless. My leaguemates pointed out it would be especially interesting to look at total points for / against and head to head matchup records. Today I will show you how to head to head records. Check out post number 1 in the series to understand total points for / against.

Pulling the Data from ESPN API

This code sets us up to get matchup data for a given year. This will include the teams matching up, the points scored by each team, and the week the matchup occurs.

A general flow of this block of code is as follows. First we connect to the API and pull data for the specified year. Like the last blog post, we will be making a separate API call for each year of data being pulled. Next we go about getting the matchup data and subsequently putting this information into a DataFrame with the desired column labels. We are then left with a single DataFrame that contains the teams matching up, the points scored by each team, and the week the matchup occurs.

All you have to do to run this code on your own is change the league_id to your own league id. I found my league id by looking in the url on the home page of the ESPN league I wanted to analyze. The url also tells you the team id when you visit a specific teams page if you are interested in pulling data for a specific franchise.

Now lets grab the data for the entire league history and concatenate all seasons together.

Week Team1 Score1 Team2 Score2 Year
0 1 1 68.0 6 103.0 2014
1 1 5 94.0 2 111.0 2014
2 1 10 105.0 7 99.0 2014
3 1 4 97.0 3 83.0 2014
4 1 9 97.0 8 72.0 2014

Transforming into H2H Records

Now that we have every matchup and score into a single table we can begin transforming this information into head to head records. This involves going through each matchup and creating a win / loss column through the use of a margin helper variable. The functions defined, Winner and Loser, are being used to check the margin and attribute a win or a loss to the competing teams. With this we can compare wins and losses when specific franchises face off against each other. Summing these up with a simple groupby command will yield the desired head to head record results.

Now for the groupby step. Here we create two separate tables, one grouped by wins and then losses, and one grouped by losses and then wins. This way we are setting ourselves up to create a full matrix of every combination of head to head records. I'll provide a singular example to explain this further. The winners DataFrame will tells us the number of wins team 1 has when matching up against team 3 in the past (9 wins). The losers DataFrame will tells us the number of losses team 1 has when matching up against team 3 in the past (6 losses). This information together can give us the head to head record between team 1 and team 3 (9-6). The code below performs this operation for all combination of matchups. The unstack and stack methods are important since it ensures we do not drop teams that did not record a win or loss against certain teams. We are then left with our final table filled with head to head matchups in the for your league's history. The table displayed shows how team 1 faired against all other teams in the league.

Team1 Team2 W L
1 1 2 9 9
2 1 3 9 6
3 1 4 6 9
4 1 5 9 5
5 1 6 6 3
6 1 7 7 2
7 1 8 3 6
8 1 9 7 3
9 1 10 7 1

The last step to this analysis is mapping team names to team ids. This way you can look through the head to head records and make sense of which fantasy manager is which. We do this with another call to the ESPN API. The code is set up to pull team names from the inputted year. The team names will be added to a list in order of their team ID numbering. I did this to make it simple when we map the team names to their respective team IDs.

This post goes into the details of calculating head to head records in ESPN league history. This is not something that can be seen through the ESPN site so taking advantage of the ESPN API is crucial to this exercise. Next week will be another league history series post in which I will talk further about matchup data. We will create some interesting plots so be sure to check it out!