获得不重叠的推拉窗的意思。

问题描述 投票:0回答:1

给定这个数据框,我试图返回行c1的每两个值的滑动窗口的平均值,且没有重叠。

df = pd.DataFrame({ 'id_val' : [1,2,3,4,5] , 'c1': [1.0 , 2.0, 3.0,4.0,5.0]})

enter image description here

我试图返回c1行中每两个值的滑动窗口的平均数 没有重叠。

的平均值 [1,2] , [3,4] , [5] = [1.5 , 3.5, 5 ]

df.rolling(2)['c1'].mean()[1::2]

返回。

enter image description here

我认为这意味着取一个大小为2的滚动窗口,计算 "c1 "的平均值。[1::2] 意思是从第二行开始,每隔一行就返回一行。这样做对吗?

为了返回[5],也就是最后一行的平均值,我可以返回数据框c1的最后值,但这看起来很笨拙,有没有更简洁的方法?

python pandas sliding-window
1个回答
0
投票

那这样呢。

df.groupby(pd.cut(df['c1'],[0,2,4,6]))['c1'].mean().reset_index(drop=True)

结果是..:

0    1.5
1    3.5
2    5.0
Name: c1, dtype: float64
© www.soinside.com 2019 - 2024. All rights reserved.