【问题标题】:How do I convert NBA-API List to DataFrame如何将 NBA-API 列表转换为 DataFrame
【发布时间】:2020-11-13 20:13:02
【问题描述】:

将 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))

【问题讨论】:

  • 没有意识到我可以为列表的索引设置一个变量并拉出数据框。感谢您的帮助。

标签: pandas list dataframe nba-api


【解决方案1】:

根据API documentation,您应该得到两个数据帧。而且由于get_data_frames方法总是输出一个DataFrames列表,你可以通过索引来分别检索它们:

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']

【讨论】:

    【解决方案2】:

    这个?

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-07
      • 2013-02-13
      • 2019-06-24
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 2020-09-01
      • 2021-11-10
      相关资源
      最近更新 更多