Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loops and Child Attributes
#1
Hi,

I'm a bit of a novice with Python in terms of creating scripts (I tend to edit / read-only) and am really struggling to understand how to use loops for accessing the Fantasy Premier League API, if this is at all the way to do it. It may be FPL, but it also helps me upskill my Python skills for work!

From the FPL API endpoint https://fantasy.premierleague.com/api/event/36/live/, my objective is to extract to CSV all the "stats" attributes, with each one as an individual header column associated to the related ID. I can only extract the "stats" (delimited) and "ID" based on the parent-child structure, but need to have the following:
Output:
ID Minutes Goals Scored Assists Clean Sheets etc. 5 90 0 0 0 1
Code so far:

import requests
import json
import numpy as np
import pandas as pd
import datetime

# Make a get request to get the latest player data from the FPL API
link = "https://fantasy.premierleague.com/api/event/36/live/"
response = requests.get(link)
# Convert JSON data to a python object
data = json.loads(response.text)
# Initialize array to hold ALL player data
# This will be a 2D array where each row is a different player
all_players = []
# Loop through each player in the data 
for i in data["elements"]:
    id = i['id']
    assists = i['assists']
    goals_scored = i['goals_scored']

# Create a 1D array of the current players stats
    individual_stats = [id, assists, goals_scored]

# Append the player array to a 2D array of all players
    all_players.append(individual_stats)
# Convert the 2D array to a numpy array
all_players = np.array(all_players)
# Convert the numpy array to a pandas dataframe (table)
dataset = pd.DataFrame({'id': all_players[:, 0], 'goals': all_players[:, 1], 'assists': all_players[:, 2]})


# Generate a unique filename based on date
filename = str(datetime.datetime.today().date()) + '_fpl_players_weekly)'

# Save the table of data as a CSV
dataset.to_csv(index=False, path_or_buf=filename)
JSON Response eg:

Output:
"elements": [ { "id": 1, "stats": { "minutes": 0, "goals_scored": 0, "assists": 0, "clean_sheets": 0, "goals_conceded": 0, "own_goals": 0, "penalties_saved": 0, "penalties_missed": 0, "yellow_cards": 0, "red_cards": 0, "saves": 0, "bonus": 0, "bps": 0, "influence": "0.0", "creativity": "0.0", "threat": "0.0", "ict_index": "0.0", "starts": 0, "expected_goals": "0.00", "expected_assists": "0.00", "expected_goal_involvements": "0.00", "expected_goals_conceded": "0.00", "total_points": 0, "in_dreamteam": false }, "explain": [ { "fixture": 351, "stats": [ { "identifier": "minutes", "points": 0, "value": 0 } ] } ] }, { "id": 2, "stats": { "minutes": 0, "goals_scored": 0, "assists": 0, "clean_sheets": 0, "goals_conceded": 0, "own_goals": 0, "penalties_saved": 0, "penalties_missed": 0, "yellow_cards": 0, "red_cards": 0, "saves": 0, "bonus": 0, "bps": 0, "influence": "0.0", "creativity": "0.0", "threat": "0.0", "ict_index": "0.0", "starts": 0, "expected_goals": "0.00", "expected_assists": "0.00", "expected_goal_involvements": "0.00", "expected_goals_conceded": "0.00", "total_points": 0, "in_dreamteam": false }, "explain": [ { "fixture": 351, "stats": [ { "identifier": "minutes", "points": 0, "value": 0 } ] } ] }
Thanks
Anthony
buran write May-10-2024, 04:04 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply


Messages In This Thread
Loops and Child Attributes - by antweeman82 - May-10-2024, 03:48 PM
RE: Loops and Child Attributes - by deanhystad - May-10-2024, 06:07 PM
RE: Loops and Child Attributes - by menator01 - May-10-2024, 06:25 PM
RE: Loops and Child Attributes - by antweeman82 - May-13-2024, 11:08 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Using one child class method in another child class garynewport 5 1,857 Jan-11-2023, 06:07 PM
Last Post: garynewport
  XML Parsing Child karthi_python 1 1,990 May-16-2019, 01:37 PM
Last Post: karthi_python
  parent/add and child/div PyMan 1 2,543 Feb-23-2018, 04:38 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020