Imagine you wanted to make an informed guess on which football teams are likely to win in the Premier League, based on last season's performance. Well, this is what I set out to do.
Extracting Data From the API
The first step I took was to access the football-data.org API website, which would be the source of my data. Now, what exactly is an API? Think of an API as a bridge between a website and your code. While the website has the data you need, you can use your computer to make requests from the site. The two systems then interact, exchange data, and deliver responses.
Once you access the website, you need to create an account, typically via your email, where an authentication code can be sent. This code allows you to access and use the data. With the code at hand, you can then proceed to install the necessary libraries on Python (I used json (to load the data), requests, and pandas(to work with tables)). The next step I took was to set the URL to match the data I wanted. Since I wanted the latest data, I filtered using year, setting it to 2024. I then made the request to access the data, which was fetched successfully.
Manipulating the Data
After fetching, one now has to read the data. The data = json.load(f) line then allowed me to convert the file from JSON to a Python dictionary. With this, I was then able to prepare the data for further analysis. The first thing I did was extract the relevant fields I needed, which included the home and away teams, the date each match was played, and the teams' respective scores.
The next thing I did was create a function that would extract the winning teams based on their scores. This, for instance, showed me that while Manchester United FC played against Fulham FC, it scored one goal. The next step entailed aggregating the appearances and wins to see the total number of games each team won.
Getting the Win Probabilities
My final step involved using the total wins per team against the total matches played to calculate the probability that they would win this season. From this, I learned that Liverpool FC had a total of 25 wins, the highest of all. This meant that it had the highest probability of being the winning team in the coming Premier League.
This was quite an interesting project!
Top comments (0)