在 Python 中按组应用加权滚动平均值时出错
Error applying a weighted rolling average by group in Python
我有以下数据框,我正在尝试为其计算加权滚动平均值:
import pandas as pd
df = pd.DataFrame({'player_ID': {0: 123,
1: 123,
2: 123,
3: 123,
4: 123,
5: 456,
6: 456,
7: 456,
8: 456,
9: 456},
'hole_sg': {0: 3.14,
1: 2.70,
2: 5.20,
3: -0.02,
4: 2.09,
5: -2.92,
6: -2.01,
7: 3.02,
8: -0.72,
9: -1.77}})
此数组表示我要应用于滚动平均值的权重:
import numpy as np
weights = np.array([0.66342043, 0.6983373 , 0.73509189])
我用来计算加权平均值的代码(如下所示)在我不分组时有效,但我希望能够按 'player_ID'
分组
df['rolling'] = df.groupby('player_ID')['hole_sg'].rolling(3).apply(lambda x: (np.dot(x, weights)/weights).sum())
当我尝试分组时出现以下错误:
TypeError: incompatible index of inserted column with frame index
有人对如何解决这个问题有任何建议吗?如果一切正常,这就是我会得到的结果:
当您 groupby
并使用 rolling
时,您会得到一个 MultiIndex。要与原始 DataFrame 对齐,您可以使用:
df["rolling"] = df.groupby('player_ID')['hole_sg'].rolling(3).apply(lambda x: (np.dot(x, weights))/weights.sum()).droplevel(0)
>>> df
player_ID hole_sg rolling
0 123 3.14 NaN
1 123 2.70 NaN
2 123 5.20 3.715635
3 123 -0.02 2.579053
4 123 2.09 2.371253
5 456 -2.92 NaN
6 456 -2.01 NaN
7 456 3.02 -0.534549
8 456 -0.72 0.117432
9 456 -1.77 0.095197
我有以下数据框,我正在尝试为其计算加权滚动平均值:
import pandas as pd
df = pd.DataFrame({'player_ID': {0: 123,
1: 123,
2: 123,
3: 123,
4: 123,
5: 456,
6: 456,
7: 456,
8: 456,
9: 456},
'hole_sg': {0: 3.14,
1: 2.70,
2: 5.20,
3: -0.02,
4: 2.09,
5: -2.92,
6: -2.01,
7: 3.02,
8: -0.72,
9: -1.77}})
此数组表示我要应用于滚动平均值的权重:
import numpy as np
weights = np.array([0.66342043, 0.6983373 , 0.73509189])
我用来计算加权平均值的代码(如下所示)在我不分组时有效,但我希望能够按 'player_ID'
分组df['rolling'] = df.groupby('player_ID')['hole_sg'].rolling(3).apply(lambda x: (np.dot(x, weights)/weights).sum())
当我尝试分组时出现以下错误:
TypeError: incompatible index of inserted column with frame index
有人对如何解决这个问题有任何建议吗?如果一切正常,这就是我会得到的结果:
当您 groupby
并使用 rolling
时,您会得到一个 MultiIndex。要与原始 DataFrame 对齐,您可以使用:
df["rolling"] = df.groupby('player_ID')['hole_sg'].rolling(3).apply(lambda x: (np.dot(x, weights))/weights.sum()).droplevel(0)
>>> df
player_ID hole_sg rolling
0 123 3.14 NaN
1 123 2.70 NaN
2 123 5.20 3.715635
3 123 -0.02 2.579053
4 123 2.09 2.371253
5 456 -2.92 NaN
6 456 -2.01 NaN
7 456 3.02 -0.534549
8 456 -0.72 0.117432
9 456 -1.77 0.095197