With another exciting Premier League season in full flow but with more surprises, this means another stressful year of Fantasy Football having to not only predict which teams will win but which games will be cancelled. In order to help better predict your team, why not try exploring the Fantasy Premier League API! I initially explored the API and used Alteryx’s Optimisation tools to help me pick a better team over my friends, but that failed completely with the highlight being getting an astouding 3 points for my triple captain. I therefore had to use the data in another way to make the most of it. Using last season’s data, I retrieved and cleansed the data in Alteryx and then built an animated bar chart in Tableau to show the top 10 most commonly selected players for each game week in Fantasy Footbll. The result is a fun visualisation which surprisingly reminisces some of the memorable story points of the 2019-20 season.
The story of 2019-20
I initially expected to just to see the same 10 most popular/best players swapping places every now and then. Unsurprisingly, there were many top players from the two best teams in Liverpool and Manchester City with players like Van Dijk, Salah, Sterling and De Bruyne often seen in the animation. However, I was surprised with how an in-form player could quickly shoot up to be the most popular player for the game week. It makes sense but I was still amazed to see how even some less fashionable players could rise to the very top. Some of these in-form players include Teemu Pukki’s early signs of being the top scorer until people realised he played for Norwich City, Tammy Abraham showing why he should start for Chelsea until Giroud started playing, Jamie Vardy’s streak of goals midway through the season, and then finally De Bruyne proving to be a no brainer selection in the post-lockdown period. The most suprising part of the animation however was the popularity of bargain players. These are players who cost very little but can get quite a few points mainly because they play matches at the end of the day. Little did I know that all my “clever” finds throughout the season were pretty much the same finds everyone else had. For example, John Lundstram wrongly being classified as a defender despite being a midfielder made him the most popular player in one game week, Mason Greenwood’s impressive goal record for a cheap youth player was another popular option, and there even was a top 10 appearance from Martin Kelly of all people.
API and Alteryx
There are plenty of helpful resources on the Internet which explain the list of available Fantasy Premier Legue APIs as well as how to connect to it, including via Alteryx. For this personal project, I used https://fantasy.premierleague.com/api/bootstrap-static/ for information on each Premier League player and https://fantasy.premierleague.com/api/element-summary/ for information on each player’s stats for each game week including the number of times they were picked by a Fantasy Premier League manager.
The first API contains the season stats for each Premier League player. By parsing the API through the Download tool and the JSON Parse tool you will be able to see stats such as the number of goals they have scored this season, the number of minutes played, and even their own name. In this particular use case, I only needed the JSON_Name values that start with “elements” because I only needed data on each player and not on the matches. Then, I transpose the data, obtain the field names from the JSON_Name field and do some additional cleansing like combining the first and surname of the players to create a clean data table like below. The field Player ID in particular is important and is derived from the JSON_Name where the value is elements.#.id where # is different for each player. This Player ID value is the key to join the data with the next API data.
The second API is actually in the format of https://fantasy.premierleague.com/api/element-summary/#/ where # is the Player ID value of the player. For example, as Mesut Ozil’s Player ID value is 1, data on Ozil will be found in the API https://fantasy.premierleague.com/api/element-summary/1/. Feel free to check out this link if you would like to see a bunch of 0s. Using the Generate Rows tool, I generated the link multiple times with the Player ID value increasing incrementally. Eventually, I downloaded 639 URLs in total as this was the number of available Fantasy Premier League players that were available at the time of writing.
Parsing the data returns a value under JSON_Name which can be split into three parts. The first part decides whether the data is related to the match or related to the player. I only needed player data so I kept data where the first value is “history” instead of “fixture”. The second value is the game week number starting with 0 for game week 1. Finally, the third value is a field name which can be anything from the number of goals scored by that player for that game week to the team that they played against. Using the Player ID from the URL, we can join this data back to the data from the first API.
By joining the two datasets together on Player ID, we now get a table where we have overall season stas for each player as we ll as various information on how the player did each game week. This table will be used in Tableau to create the animated bar chart. The key field that we would like to use in the animated visualisation is the “selected” value under Field. This is the number of times that the player was in someone’s Fantasy team for that particular game week.
I was able to build the animated bar chart in Tableau thanks to an article written by Ewe Zi Yi which can be found here.
I highly recommend using this article if you would like to create an animated bar chart of your own in Tableau. I recommend following the article step by step including using the data that Ewe uses to ensure that you get the correct output. Then, you will have a good level of understanding to start inputting your own data and tweaking the animation to suit your needs. The overall gist of the visualisation involves splitting up your time related data points into equal parts so that Tableau creates a staggered animation as the values go from one data point to another, or from one game week to the next in this case. I split each game week into 10 parts which meant that the bar charts will make 10 movements before getting from the value of one game week to the value of the next game week. I found that 10 was a good balance of keeping the animation smooth enough without being too slow that it is boring.
I’ve also made my animation a bit more colourful because I can’t be seen as boring if I’m posting my video on YouTube! I’ve included the team colour to the bars and used the players’ faces, courtesy of the official Premier League website, to label the bars. I found the pictures of the players especially useful in identifying each bar. Unfortunately, that might mean more about how much football I’ve been watching on the T.V. lately than my data visualisation skills.
Animated bar charts and Fantasy Premier League football is probably not the most ground-breaking data analysis you could be doing out there but it certainly is good fun. I can’t wait to use some of the other available data and start plugging it into the bar charts. More exctingly, I can’t wait to see what the animation will look like once this season finishes! Harry Kane, Heung-Min Son, Bruno Fernandes and Mohammed Salah are definitely some of my guesses. I would love to hear your thoughts on the animation and please let me know if you would like me to use some of the other available Fantasy Football data to create a new video.