如何将 NBA-API 列表转换为 DataFrame

How do I convert NBA-API List to DataFrame

在将 NBA-API 对象转换为 DataFrame 时出现问题。我得到的是数据框列表。如何将 DataFrame 拉出列表或跳过列表并创建 DataFrame。

## NBA API endpoints needed to obtain data
import nba_api.stats.endpoints
from nba_api.stats.static import players
from nba_api.stats.static import teams
from nba_api.stats.endpoints import shotchartdetail

import pandas as pd
from pandas import DataFrame
import matplotlib as mpl
import matplotlib.pyplot as plt

BBplayers = players.get_players()
BBteams = teams.get_teams()

print(type(players))
print(BBplayers[0])
print(len(BBplayers))
## LEN PLAYERS = 4501  PLAYER TYPE IS DICTIONARIES INSIDE LIST


Durant = [player for player in BBplayers if player['full_name'] == 'Kevin Durant'][0]
Durant_id = Durant['id']
print(Durant_id)
## Durant ID = 201142

Thunder = [name for name in BBteams if name['full_name']=='Oklahoma City Thunder'][0]
Thunder_id = Thunder['id']
print(Thunder_id)
print(type(Thunder_id))
## Thunder ID = 1610612760

DurantShotsChart = shotchartdetail.ShotChartDetail(player_id='201142',team_id=1610612760)

print(DurantShotsChart)

NewDF=DurantShotsChart.get_data_frames()

print(type(NewDF))
print(NewDF[1])
print(len(NewDF))

这个?

df1=NewDF[0]
df2=NewDF[1]

根据 API documentation 你应该得到两个数据帧。由于 get_data_frames 方法总是输出 DataFrame 列表,您可以通过索引单独检索它们:

league_averages = NewDF[0]

其中包含 ['GRID_TYPE', 'SHOT_ZONE_BASIC', 'SHOT_ZONE_AREA', 'SHOT_ZONE_RANGE', 'FGA', 'FGM', 'FG_PCT'].

然后

shot_chart_detail= NewDf[1]

其中有以下列['GRID_TYPE', 'GAME_ID', 'GAME_EVENT_ID', 'PLAYER_ID', 'PLAYER_NAME', 'TEAM_ID', 'TEAM_NAME', 'PERIOD', 'MINUTES_REMAINING', 'SECONDS_REMAINING', 'EVENT_TYPE', 'ACTION_TYPE', 'SHOT_TYPE', 'SHOT_ZONE_BASIC', 'SHOT_ZONE_AREA', 'SHOT_ZONE_RANGE', 'SHOT_DISTANCE', 'LOC_X', 'LOC_Y', 'SHOT_ATTEMPTED_FLAG', 'SHOT_MADE_FLAG', 'GAME_DATE', 'HTM', 'VTM']