类型错误:列表索引必须是整数或片,而不是非类型如何修复以完成数据帧?

2024-09-27 21:33:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正试图为2019赛季的所有CFB球员建立一个数据框架。我使用SportsReference API,我可以提取单个玩家的数据帧,但是我在将所有玩家添加到一个大数据帧时遇到了一个问题。这是我的代码:

# Build 2019 dataframe
for team in tqdm(Teams(year)):
    roster = team.roster
    for player in roster.players:
        data = pd.Series([player.player_id,player.name,player.position,player.season,player.team_abbreviation,player.games,player.points,player.total_touchdowns,player.yards_from_scrimmage,player.yards_from_scrimmage_per_play,player.plays_from_scrimmage,
                    player.passing_completion,player.completed_passes,player.pass_attempts,player.passing_yards,player.adjusted_yards_per_attempt, 
                    player.passing_yards_per_attempt,player.passing_touchdowns,player.interceptions_thrown,player.quarterback_rating,
                    player.rush_attempts,player.rush_yards,player.rush_yards_per_attempt,player.rush_touchdowns,
                    player.receptions,player.receiving_yards,player.receiving_yards_per_reception,player.receiving_touchdowns, 
                    player.rushing_and_receiving_touchdowns,player.other_touchdowns,player.kickoff_return_touchdowns,player.punt_return_touchdowns, 
                    player.extra_points_made,player.field_goals_made,player.two_point_conversions, 
                    player.solo_tackles,player.assists_on_tackles,player.tackles_for_loss,player.total_tackles, 
                    player.sacks,player.safeties,player.passes_defended,player.fumbles_forced,player.fumbles_recovered,player.interceptions,
                    player.yards_recovered_from_fumble,player.yards_returned_from_interceptions,player.yards_returned_per_interception,
                    player.fumbles_recovered_for_touchdown,player.interceptions_returned_for_touchdown], index=df.columns, name='a').fillna(method='ffill')
        df.append(data)

对于变量的数量,请提前道歉。当我运行代码时,它返回错误:

TypeError: list indices must be integers or slices, not NoneType

我已尝试更改索引类型,但无法修复它。为了提供帮助,以下是我构建数据框架的方式:

ind = pd.Index(['player_id','name','position','season','team_abbreviation','games','points', 'total_touchdowns','yards_from_scrimmage','yards_from_scrimmage_per_play','plays_from_scrimmage',# basics
                   'passing_completion','completed_passes','pass_attempts','passing_yards','adjusted_yards_per_attempt', #passingstats
                    'passing_yards_per_attempt','passing_touchdowns','interceptions_thrown','quarterback_rating',
                    'rush_attempts','rush_yards', 'rush_yards_per_attempt','rush_touchdowns', #rushing stats
                    'receptions','receiving_yards','receiving_yards_per_reception','receiving_touchdowns', #receiving stats
                    'rushing_and_receiving_touchdowns','other_touchdowns','kickoff_return_touchdowns','punt_return_touchdowns', #special TDs
                    'extra_points_made','field_goals_made','two_point_conversions', #=<3 point stats
                    'solo_tackles','assists_on_tackles','tackles_for_loss','total_tackles', #defensive stats
                    'sacks','safeties','passes_defended','fumbles_forced','fumbles_recovered','interceptions',
                    'yards_recovered_from_fumble', 'yards_returned_from_interceptions','yards_returned_per_interception',
                    'fumbles_recovered_for_touchdown','interceptions_returned_for_touchdown'])
df = pd.DataFrame(columns = ind)

Tags: fromforrecoveredplayerperrushattemptpassing

热门问题