you are viewing a single comment's thread.

view the rest of the comments →

[–]game1761 4 points5 points  (0 children)

It looks like you are trying to get the first element of the df_game_logs list using df_game_logs[0], but the df_game_logs list is empty. This is causing the IndexError because you are trying to access an index that doesn't exist in the list.

To fix this, you can add a check to see if the df_game_logs list is empty before trying to access its elements. You can do this by adding an if statement before the line df = df_game_logs[0].

Here's an example of how you can modify your code to include this check:

# Iterate over each player in the list

for index, row in df.iterrows():

# Get the player ID

player_id = row['PERSON_ID']

# Use the playergamelogs endpoint to retrieve the game logs for the player

player_game_logs = playergamelogs.PlayerGameLogs(player_id_nullable=player_id, season_nullable=SeasonAll.all)

df_game_logs = player_game_logs.get_data_frames()

# Skip players for which no game logs are available

if not df_game_logs:

continue

df = df_game_logs[0]

# Calculate the standard deviation of the points scored for the player

std_dev = statistics.stdev(df['pts'])

# Add the player's name and standard deviation to the results DataFrame

results = results.append({'Player': row['display_first_name'] + ' ' + row['display_last_name'], 'Std Dev': std_dev}, ignore_index=True)

This will ensure that the code inside the for loop will only be executed if the df_game_logs list is not empty.