A simple & effective framework to build a custom Fantasy Premier League Analysis Dashboard in Python using Dataframes and PandasGUI.
GUEST WRITER #1 – Shantanu Sharma
Senior Consultant at RegCentric – an Australia based data consulting firm.
On that positive note, let’s start!
Build a Custom Fantasy Premier League Analysis Dashboard in Python using Dataframes and PandasGUIFor Fantasy Premier League (FPL) fans, data analysis along with gut feel play a key role in selecting the right players each game week.
I have been playing this game for 8 seasons now and am totally smitten by the model of the game. Another facet that absorbs me is the language of Python and the extent of data analyses and manipulation which can be attained by it’s usage.
This is an endeavour to apply both Python and Fantasy Premier League API to understand the data readily available. Also, how can a typical FPL enthusiast create a custom model for self-analysis which can aid in creating a competitive data-driven team.
Minimum prerequisite to proceed is installation of Python and an editor. (I use Visual Studio Code)
Go ahead and create a project folder and 3 files (main.py, __init__.py, Pipfile) in it.
The structure of the project would look like this:
1) main.py file will hold our python executable code.
2) __init__.py file makes Python treat directories containing it as modules. __init__.py file will be kept empty in the folder.
3) Pipfile is used to manage the library requirements.
PYTHON LIBRARY REQUIREMENTS
Open Pipfile and type in the requirements given below and save. Please put in the Python version installed on your system.
The Pipfile manages all our requirements at one place and we need not individually install them for the project.
1) Requests is used to read the API url.
2) Pandas is used for Data Manipulation.
3) PandasGUI is used for the visualisation.
Now we need to go the terminal, point to the project folder and type in pipenv shell (you might have to run pip install pipenv if it is not installed).
This command will load the requirements from the Pipfile and create a Pipfile.lock in your project folder.
Run pipenv shell to activate the project virtual environment.
PULLING DATA AND MODEL
Let’s go to the main.py file and import the libraries.
Next step is to pull the data from the API.
Now we have the data available to us in json structure.
Command print(json.keys()) would show you the tables returned.
dict_keys([‘events’, ‘game_settings’, ‘phases’, ‘teams’, ‘total_players’, ‘elements’, ‘element_stats’, ‘element_types’])
For this demonstration we would be looking at elements table. This table has 67 columns. I have handpicked a few key columns to showcase.
(first_name, second_name, now_cost, selected_by_percent, total_points, points_per_game, goals_scored, assists, bps, penalties_order, corners_and_indirect_freekicks_order, direct_freekicks_order, news)
We now can use Pandas dataframes to select the columns from the elements table and create a new dataframe called ‘stats_df’.
show(stats_df) will render and open the PandasGUI.
Using filters tab, you can slice the data as required. I have used an example to demonstrate the use of filters.
Here, I am looking for a player whose cost < 8m with a bonus point system score > 400 and takes free kicks & corners.
PandasGUI has other tabs which give you option for plotting graphs as well.
You can use this code to access other columns and create a custom model which fits your need.
Fantasy Premier League is an intriguing game which demands discipline and heaps of planning. This is just one attempt to keep things simple – to learn and hopefully easy to execute.