python - 如何在 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
解决方案
推荐阅读
- javascript - Google 日历事件不会使用 Apps 脚本 deleteEvent() 方法删除
- c# - 如何创建 x 数量的任务,例如 list.Count 的依赖?
- r - 如何在ioslides R-markdown中用鼠标从幻灯片滚动到幻灯片
- python - Websocket Permessage-deflate 未发生在服务器-> 客户端方向
- javascript - 使用第 n 个 REST 调用为 Web Worker 的 onmessage 事件创建一个 Promise
- c# - Microsoft Bot Framework Composer 与 Bot Framework SDK 的集成
- html - 为什么浏览器没有下载html锚标签href中设置的文件?
- reactjs - 如何提供我的
使用在客户端和服务器渲染上构建不同并且由 和它的声音? - reactjs - 我应该返回 true 以清除 .map() 期望在箭头函数警告结束时返回一个值吗?
- python - 在 Django Admin 中直接添加内联外键