You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法查看nba_api的DataFrame,报错'list' object has no attribute 'head'

解决nba_api返回列表无法调用head()的问题

错误原因

leagueleaders.LeagueLeaders的get_data_frames()方法返回的是列表对象,而head()是pandas DataFrame的专属方法,列表没有这个属性,所以调用df17.head()会报错'list' object has no attribute 'head'。

解决方法

get_data_frames()返回的列表中,第一个元素就是我们需要的球员数据DataFrame,只需通过索引[0]提取即可:

修改后的代码:

import pandas as pd 
import numpy as np 
import seaborn as sns
import matplotlib.pyplot as plt 
import nba_api

from nba_api.stats.endpoints import leagueleaders
stats = leagueleaders.LeagueLeaders(season='2017-18')
# 提取列表中的第一个DataFrame
df17 = stats.get_data_frames()[0] 
df17.head()

验证方法

可以先打印返回结果的类型和长度,确认结构:

df_list = stats.get_data_frames()
print(type(df_list))  # 输出 <class 'list'>
print(len(df_list))   # 输出 1,说明列表里只有一个DataFrame

内容的提问来源于stack exchange,提问作者Emir Johnson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 18:24:41