Pandas中特定列数值的3周滚动平均值

huangapple go评论65阅读模式
英文:

Rolling average Pandas for 3 week period for specific column values

问题

我有一个名为'qbPast'的数据框,其中包含了一赛季的NFL球员数据。

我尝试创建一个新的滚动平均,基于"Points"列,对于每个个体球员,每3周一次,前两周应返回该周的得分,之后应返回3周滚动期间的平均值,例如球员A得分20,30,40,30,40,平均值应返回20,30,30,33.3等。

我的尝试如下:# qbPast['Avg'] = qbPast.groupby('Player')['Points'].rolling(3).mean().reset_index(drop=True)

问题是它只返回了所有球员的3周平均值,我需要按球员筛选,以便为每个球员返回滚动平均值,其他球员不应影响滚动平均值。

英文:

I have a dataframe 'qbPast' which contains nfl player data for a season.

P	Player	Week	Team	Opp	Opp Rank	Points	Def TD	Def INT	Def Yds/att	Year
2	QB	Kyler Murray	2	ARI	MIN	14	38.10	1.8125	1.0000	6.9	2021
3	QB	Lamar Jackson	2	BAL	KC	6	37.26	1.6875	0.9375	7	2021
5	QB	Tom Brady	2	TB	ATL	28	30.64	1.9375	0.7500	6.8	2021

I am attempting to create a new rolling average based on the "Points" column for each individual player for each 3 week period, for the first two weeks it should just return the points for that week and after that it should return the average for the 3 week moving period e,g Player A scores 20,30,40,30,40 the average should return 20,30,30,33.3 etc.

My attempt # qbPast['Avg'] = qbPast.groupby('Player')['Points'].rolling(3).mean().reset_index(drop=True)

The problem is it is only returning the 3 week average for all players I need it to filter by player so that it returns the rolling average for each player, the other players should not affect the rolling average.

答案1

得分: 1

问题在于索引对齐。不要使用 reset_index,而是删除第零索引级,然后将值分配给一列。

qbPast['Avg'] = qbPast.groupby('Player').rolling(3)['Points'].mean().droplevel(0)
英文:

The problem is index alignment. Don't use reset_index instead drop the zeroth index level then assign the values to a column

qbPast['Avg'] = qbPast.groupby('Player').rolling(3)['Points'].mean().droplevel(0)

答案2

得分: 1

.reset_index(drop=True)更改为.reset_index(0, drop=True),以使玩家的索引不混在一起。

英文:

You have to change the .reset_index(drop=True) into .reset_index(0, drop=True) so it is not mixing the players indices together.

答案3

得分: 0

使用groupby - transform是避免索引问题的一个不错的方法:

qbPast['avg'] = (qbPast.
             sort_values('Week').
             groupby('Player')['Points'].
             transform(lambda g: g.rolling(3).mean()))
英文:

A nice way of avoiding the index problem is using groupby - transform:

qbPast['avg'] = (qbPast.
             sort_values('Week').
             groupby('Player')['Points'].
             transform(lambda g: g.rolling(3).mean()))

huangapple
  • 本文由 发表于 2023年2月8日 22:49:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387489.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定