![]() |
Loops and Child Attributes - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Loops and Child Attributes (/thread-42106.html) |
Loops and Child Attributes - antweeman82 - May-10-2024 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: 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: ThanksAnthony RE: Loops and Child Attributes - deanhystad - May-10-2024 You can simplify the code that makes the dataframe: # make a list of players players = [] for element in elements: player = {"id", element["id"]} # Makes a dictionary for the player player.update{element.stats} # Adds all stats to the player. players.append(player) # Make dataframe containing all players and stats. dataset = pd.DataFrame(players) # Can make dataframe that has particular stats offense = dataset[["id", "goals_scored", "assists"] RE: Loops and Child Attributes - menator01 - May-10-2024 I took a little different approach # Do imports import requests, json import pandas as pd from datetime import datetime import os # Get the path of the working script path = os.path.realpath(os.path.dirname(__file__)) # The link link = 'https://fantasy.premierleague.com/api/event/36/live/' # Get response response = requests.get(link) # Covert to json format data = json.loads(response.text) # Create the column headers columns = [element for element in data['elements'][0]['stats']] # # Empty list stats = [] # Get the data for element in data['elements']: stats.append([element['stats'][key] for key in element['stats']]) # # Create the dataframe dataset = pd.DataFrame(stats, columns=columns) # # Create the filename filename = f'{path}/{datetime.today().date()}_fpl_players_weekly' # # Convert to csv dataset.to_csv(index=False, path_or_buf=filename) RE: Loops and Child Attributes - antweeman82 - May-13-2024 Great thanks @menator01 - this works perfectly! |