我正在使用下面的数据集,但我在根据球队id计算总分时遇到了麻烦.一支球队可以是主场,也可以是客场,我正在计算他们得分的连续总分.

我已经成功创建了基于主场ID和客场ID的 run 总数以及基于主场/客场ID的 run 平均值,但我很难根据这两列进行计算

例如,如果在第1场比赛中主队得分1分,然后在第2场比赛中他们是客场球队并且得分3分,我想创建一个列,表明截至数据集中的此时他们总共得分4分

到目前为止我的代码是:

import pandas as pd

game_data = pd.read_csv('game_data.csv')

game_data['home_avg_home_games'] = game_data.groupby('home_id')['home_score'].transform(lambda x: x.rolling(165, min_periods = 0).mean())
game_data['home_avg_against_home_games'] = game_data.groupby('home_id')['away_score'].transform(lambda x: x.rolling(165, min_periods = 0).mean())

game_data['away_avg_away_games'] = game_data.groupby('away_id')['away_score'].transform(lambda x: x.rolling(165, min_periods = 0).mean())
game_data['away_avg_against_away_games'] = game_data.groupby('away_id')['home_score'].transform(lambda x: x.rolling(165, min_periods = 0).mean())

game_data['scored_home_total'] = game_data.groupby('home_id')['home_score'].cumsum()
game_data['scored_away_total'] = game_data.groupby('away_id')['away_score'].cumsum()
game_id away_id home_id away_score home_score home_avg_home_games home_avg_against_home_games away_avg_away_games away_avg_against_away_games
446877 138 134 1 4 4 1 1 4
446911 141 139 5 3 3 5 5 3
446873 121 118 3 4 4 3 3 4
446875 137 158 12 3 3 12 12 3
446872 142 110 2 3 3 2 2 3
446876 136 140 2 3 3 2 2 3
446874 143 113 2 6 6 2 2 6
446879 120 144 4 3 3 4 4 3
446871 119 135 15 0 0 15 15 0
446878 141 139 5 3 3 5 5 3
446869 115 109 10 5 5 10 10 5
446889 112 108 9 0 0 9 9 0
446868 145 133 4 3 3 4 4 3
446870 117 147 5 3 3 5 5 3
446867 111 114 6 2 2 6 6 2
446896 121 118 2 0 2 2.5 2.5 2
446910 138 134 5 6 5 3 3 5
446887 141 139 2 3 3 4 4 3
446883 116 146 8 7 7 8 8 7
446886 136 140 10 2 2.5 6 6 2.5
446885 137 158 2 1 2 7 7 2
446882 115 109 6 11 8 8 8 8
446880 112 108 6 1 0.5 7.5 7.5 0.5
446881 145 133 5 4 3.5 4.5 4.5 3.5
446884 119 135 3 0 0 9 9 0
446901 141 139 3 5 3.5 3.75 3.75 3.5
446898 137 158 3 4 2.666666667 5.666666667 5.666666667 2.666666667
446899 136 140 9 5 3.333333333 7 7 3.333333333
446891 115 109 4 3 6.333333333 6.666666667 6.666666667 6.333333333

我想要的输出是:

game_id away_id home_id away_score home_score home_avg_for_home_games home_avg_against_home_games away_avg_away_games away_avg_against_away_games home_total_score away_total_score
446877 1 2 1 4 4 1 1 4 4 1
446911 2 3 5 3 3 5 5 3 3 5
446873 1 3 3 4 3.5 4 2 4 7 4

推荐答案

这将帮助您解决您的问题.然而,它给出了避免警告,警告你需要使用include_groups=False,但如果你,它会删除home_idaway_id

game_data = game_data.groupby('home_id',group_keys=False,as_index=False).apply(lambda x: x.assign(home_total_score=x['home_score'].cumsum()),include_groups=True)
game_data= game_data.groupby('away_id',group_keys=False,as_index=False).apply(lambda x: x.assign(away_total_score=x['away_score'].cumsum()),include_groups=True)

我用np.where()创建了新的away_id来解决这个问题.所以您需要在代码中添加import numpy as np.

new = game_data.groupby('home_id',group_keys=False,as_index=False).apply(lambda x: x.assign(home_total_score=x['home_score'].cumsum()),include_groups=False)
new= game_data.groupby('away_id',group_keys=False,as_index=False).apply(lambda x: x.assign(away_total_score=x['away_score'].cumsum()),include_groups=False)

new["away_id"] = np.where(new["game_id"] == game_data["game_id"],game_data["away_id"],None)

对于重新索引列,添加这些行代码.因为我们创建了新列并将其添加到上一个索引中.

columns = new.columns.tolist()

columns.insert(columns.index('away_score'), columns.pop(columns.index('away_id')))

new = new.reindex(columns=columns)

Python相关问答推荐

Pandas :多索引组

根据网格和相机参数渲染深度

比较两个二元组列表,NP.isin

如何使用scipy从频谱图中回归多个高斯峰?

如何在箱形图中添加绘制线的传奇?

删除最后一个pip安装的包

如何在类和classy-fastapi -fastapi- followup中使用FastAPI创建路由

在Python中管理打开对话框

如何调整QscrollArea以正确显示内部正在变化的Qgridlayout?

在Python中,从给定范围内的数组中提取索引组列表的更有效方法

Python脚本使用蓝牙运行在Windows 11与raspberry pi4

如何使regex代码只适用于空的目标单元格

手动设置seborn/matplotlib散点图连续变量图例中显示的值

Python避免mypy在相互引用中从另一个类重定义类时失败

如何过滤组s最大和最小行使用`transform`'

如何合并具有相同元素的 torch 矩阵的行?

如何获得3D点的平移和旋转,给定的点已经旋转?

用由数据帧的相应元素形成的列表的函数来替换列的行中的值

如果服务器设置为不侦听创建,则QWebSocket客户端不连接到QWebSocketServer;如果服务器稍后开始侦听,则不连接

将相应的值从第2列合并到第1列(Pandas )