首页 > 解决方案 > 如何在 python 中修复此错误:传递值的形状为 (20, 32),索引暗示 (20, 38)

问题描述

我在执行我的代码时遇到问题。该代码旨在根据比赛统计数据(保存在数据框中)获得每支球队得分和失球的值。错误信息是:ValueError: Shape of passed values is (20, 32), indices imply (20, 38)。我也不完全理解代码,因为我是从另一个资源中获得的,而且我对 python 也不太熟悉,如果可以解释的话那就太棒了。

取得进球:

 # Gets the goals scored agg arranged by teams and matchweek
def get_goals_scored(playing_stat):
# Create a dictionary with team names as keys
teams = {}
for i in playing_stat.groupby('HomeTeam').mean().T.columns:
    teams[i] = []

# the value corresponding to keys is a list containing the match location.
for i in range(len(playing_stat)):
    HTGS = playing_stat.iloc[i]['FTHG']
    ATGS = playing_stat.iloc[i]['FTAG']
    teams[playing_stat.iloc[i].HomeTeam].append(HTGS)
    teams[playing_stat.iloc[i].AwayTeam].append(ATGS)

# Create a dataframe for goals scored where rows are teams and cols are matchweek.
GoalsScored = pd.DataFrame(data=teams, index = [i for i in range(1,39)]).T
GoalsScored[0] = 0
# Aggregate to get uptil that point
for i in range(2,39):
    GoalsScored[i] = GoalsScored[i] + GoalsScored[i-1]
return GoalsScored

获得失球:

# Gets the goals conceded agg arranged by teams and matchweek
def get_goals_conceded(playing_stat):
# Create a dictionary with team names as keys
teams = {}
for i in playing_stat.groupby('HomeTeam').mean().T.columns:
    teams[i] = []

# the value corresponding to keys is a list containing the match location.
for i in range(len(playing_stat)):
    ATGC = playing_stat.iloc[i]['FTHG']
    HTGC = playing_stat.iloc[i]['FTAG']
    teams[playing_stat.iloc[i].HomeTeam].append(HTGC)
    teams[playing_stat.iloc[i].AwayTeam].append(ATGC)

# Create a dataframe for goals scored where rows are teams and cols are matchweek.
GoalsConceded = pd.DataFrame(data=teams, index = [i for i in range(1,39)]).T
GoalsConceded[0] = 0
# Aggregate to get uptil that point
for i in range(2,39):
    GoalsConceded[i] = GoalsConceded[i] + GoalsConceded[i-1]
return GoalsConceded

标签: python

解决方案


推荐阅读