05 Apr 2021 - Eloy Chang
This time I just wanted to know about the performance not just in the last gameweek, but all the season so far.
For this I have to use two other endpoints of the FPL API, the fixtures endpoint and the element endpoint.
This endpoint has information about every scheduled fixture in the actual season of the premier league, like gameweek, home team, away team, date and time, and a level of difficulty estimated to the both teams involved.
In this case I just wanted to know the gameweek of each fixture, to download the fixture a new method was added to the tableau_data
class
def _get_fixtures_data(self):
url = "https://fantasy.premierleague.com/api/fixtures/"
r = requests.get(url.format(id))
json = r.json()
self.fixtures = pd.DataFrame(json)[["id", "event"]].set_index("id")
This endpoint has all the relevant information of each player in the premier league, a lot of useful information for future analysis like, goals, assists, clean sheets, how many trainers bought the player and how many sold it, etc. All open by fixtures.
This time I will focus just on points scored on each gameweek, to download this data I create the get_historical_data
method.
def get_historical_data(self):
self._get_fixtures_data()
self.history = pd.DataFrame()
columns = ["fixture", "total_points"]
url = " https://fantasy.premierleague.com/api/element-summary/{}/"
for id, player in self.elements.iterrows():
r = requests.get(url.format(id))
json = r.json()
fixtures = pd.DataFrame(json['history'])[columns].set_index("fixture")
fixtures = fixtures.join(self.fixtures)
fixtures["player_id"] = id
self.history = self.history.append(fixtures)
And the resulting dashboard was this…