Pandas 我应该如何对另一个分类变量的每个值执行滚动平均值?
下面的for循环运行得非常慢,但它包含了我尝试执行的要点。 对于变量“category”的每个值,我想计算当前行(不包括当前行)之前所有行的“y_all_reg”列的移动平均值。计算值在下面复制的代码中命名为“encoded” 我应该如何使用熊猫中的矢量化操作来实现这一点Pandas 我应该如何对另一个分类变量的每个值执行滚动平均值?,pandas,Pandas,下面的for循环运行得非常慢,但它包含了我尝试执行的要点。 对于变量“category”的每个值,我想计算当前行(不包括当前行)之前所有行的“y_all_reg”列的移动平均值。计算值在下面复制的代码中命名为“encoded” 我应该如何使用熊猫中的矢量化操作来实现这一点 df['encoded'] = 0 # df is already sorted by 'datetime' categories = df['category'].unique() for r in categories:
df['encoded'] = 0 # df is already sorted by 'datetime'
categories = df['category'].unique()
for r in categories:
subdf = df.loc[df.category == r, 'y_all_reg']
df.loc[df.category == r, 'encoded'] = \
subdf.expanding().mean() - subdf / subdf.expanding().count()
IIUC,您需要
expansing().mean()
和shift()
:
选项2:您还可以分别执行扩展().mean()
和移位()
:
g = df.groupby('category')
df['encoded'] = g['y_all_reg'].expanding().mean().reset_index(level=0, drop=True)
df['encoded'] = g['encoded'].shift()
选项3:使用较大的数据集和类别计数,您可以手动计算滚动平均值:
g = df.groupby('category')
s = g['y_all_reg'].agg(['cumsum','cumcount'])
df['encoded'] = s['cumsum']/s['cumcount'].add(1)
df['encoded'] = g['encoded'].shift()
数据:
np.random.seed(1)
df = pd.DataFrame({'category': np.random.randint(0,2,10),
'encoded': np.random.uniform(0,1,10)})
category y_all_reg encoded
0 1 0.092339 NaN
1 1 0.186260 0.092339
2 0 0.345561 NaN
3 0 0.396767 0.345561
4 1 0.538817 0.139299
5 1 0.419195 0.272472
6 1 0.685220 0.309153
7 1 0.204452 0.384366
8 1 0.878117 0.354380
9 0 0.027388 0.371164
输出:
np.random.seed(1)
df = pd.DataFrame({'category': np.random.randint(0,2,10),
'encoded': np.random.uniform(0,1,10)})
category y_all_reg encoded
0 1 0.092339 NaN
1 1 0.186260 0.092339
2 0 0.345561 NaN
3 0 0.396767 0.345561
4 1 0.538817 0.139299
5 1 0.419195 0.272472
6 1 0.685220 0.309153
7 1 0.204452 0.384366
8 1 0.878117 0.354380
9 0 0.027388 0.371164
性能:在10000
行上测试10
类别:
Option 1: 7.81 ms ± 98.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Option 2: 8.13 ms ± 248 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Option 3: 5.96 ms ± 261 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
IIUC,您需要
expansing().mean()
和shift()
:
选项2:您还可以分别执行扩展().mean()
和移位()
:
g = df.groupby('category')
df['encoded'] = g['y_all_reg'].expanding().mean().reset_index(level=0, drop=True)
df['encoded'] = g['encoded'].shift()
选项3:使用较大的数据集和类别计数,您可以手动计算滚动平均值:
g = df.groupby('category')
s = g['y_all_reg'].agg(['cumsum','cumcount'])
df['encoded'] = s['cumsum']/s['cumcount'].add(1)
df['encoded'] = g['encoded'].shift()
数据:
np.random.seed(1)
df = pd.DataFrame({'category': np.random.randint(0,2,10),
'encoded': np.random.uniform(0,1,10)})
category y_all_reg encoded
0 1 0.092339 NaN
1 1 0.186260 0.092339
2 0 0.345561 NaN
3 0 0.396767 0.345561
4 1 0.538817 0.139299
5 1 0.419195 0.272472
6 1 0.685220 0.309153
7 1 0.204452 0.384366
8 1 0.878117 0.354380
9 0 0.027388 0.371164
输出:
np.random.seed(1)
df = pd.DataFrame({'category': np.random.randint(0,2,10),
'encoded': np.random.uniform(0,1,10)})
category y_all_reg encoded
0 1 0.092339 NaN
1 1 0.186260 0.092339
2 0 0.345561 NaN
3 0 0.396767 0.345561
4 1 0.538817 0.139299
5 1 0.419195 0.272472
6 1 0.685220 0.309153
7 1 0.204452 0.384366
8 1 0.878117 0.354380
9 0 0.027388 0.371164
性能:在10000
行上测试10
类别:
Option 1: 7.81 ms ± 98.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Option 2: 8.13 ms ± 248 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Option 3: 5.96 ms ± 261 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
您正在查找GroupBy请显示您的数据帧和预期输出的示例您正在查找GroupBy请显示您的数据帧和预期输出的示例我的目的是将“encoded”初始化为0,然后设置为其类别下y_all_reg的滚动平均数。我不确定你的是否也一样。是的,是的。您只需在之后填写即可。查看选项1。由于encoded被初始化为0,df.groupby('category')['encoded'].transform(lambda x:x.expansing().mean().shift())不是为每一行生成零吗?我把列名搞乱了。请参阅更新的列。我的目的是将“encoded”初始化为0,然后将其设置为其类别下y_all_reg的滚动平均值。我不确定你的是否也一样。是的,是的。您只需在之后填写即可。查看选项1。由于encoded被初始化为0,df.groupby('category')['encoded'].transform(lambda x:x.expansing().mean().shift())不是为每一行生成零吗?我把列名搞乱了。请参阅更新的列。