如何修复 Python 中的此错误:传递的值的形状为 (20, 32),索引暗示为 (20, 38)

2024-09-25 08:42:03 发布

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

我在执行代码时遇到问题。该代码旨在根据比赛统计数据(保存在数据框中)为每支球队获得得分和失球的值。错误消息是: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

Tags: andtheinforgetrangestatgoals