如何正确获取nba投篮图数据?

How to get nba shot chart data correctly?

我想从 nba 网站获取数据来创建投篮图。我从这个网站 https://datavizardry.com/2020/01/28/nba-shot-charts-part-1/?fbclid=IwAR3BheHQkSAmCJRr_z7ux1ygbspLjLdrvTRjWAVHOrr2BPvVh7jsIos_e9w 得到它,我的代码如下所示:

from nba_api.stats.endpoints import shotchartdetail
import json
import pandas as pd

response = shotchartdetail.ShotChartDetail(
    team_id=0,
    player_id=0,
    season_nullable='2001-02',
    season_type_all_star='Regular Season'
)

content = json.loads(response.get_json())

results = content['resultSets'][0]
headers = results['headers']
rows = results['rowSet']
df = pd.DataFrame(rows)
df.columns = headers

# write to csv file
df.to_csv(r'C:\Users\Comp\Desktop\nba_2001-02.csv', index=False)

它正在工作,但在这篇文章中,当我从一开始就下载示例数据时,数据中的作者也获得了有关未命中镜头的信息,而当我 运行 我的代码时,我只获得了有关已拍摄镜头的数据。是否也可以获取有关投篮不中的信息,或者 nba 不再共享此数据?

默认情况下(很可能自文章发布以来已更新)包设置为仅 return 得分的投篮,因此您需要将该参数更改为不是得分,而是投篮尝试次数,因为这将 return 投篮和投篮失误:

response = shotchartdetail.ShotChartDetail(
    team_id=0,
    player_id=0,
    season_nullable='2001-02',
    context_measure_simple = 'FGA', #<-- Default is 'PTS' and will only return made shots, but we want all shot attempts
    season_type_all_star='Regular Season'
)

此外,我会在数据框中再添加一件事,这样您就可以按名称而不是数字来获得列:

results = content['resultSets'][0]
headers = results['headers']
rows = results['rowSet']
df = pd.DataFrame(rows, columns=headers) #<-- add the columns parameter
df.columns = headers