在360度范围内计算移动平均数

3

这是我的数据

degree, value

0.0,0.42105263157894735
1.0,0.47368421052631576
2.0,0.47368421052631576
3.0,0.47368421052631576
4.0,0.5
5.0,0.5
6.0,0.5
7.0,0.47368421052631576
8.0,0.47368421052631576
9.0,0.47368421052631576
10.0,0.39473684210526316
..............
350.0,0.5263157894736842
351.0,0.5526315789473685
352.0,0.47368421052631576
353.0,0.47368421052631576
354.0,0.47368421052631576
355.0,0.4473684210526316
356.0,0.4473684210526316
357.0,0.4473684210526316
358.0,0.42105263157894735
359.0,0.42105263157894735

因此,它是从0度到359度的圆形。
我想使用这些数值构建移动平均。 我用以下方法实现:

df['smoothing'] = df['value'].rolling(window=10).mean()   

    degree      value  smoothed
0      0.0   0.526316       NaN
1      1.0   0.000000       NaN
2      2.0   0.000000       NaN
3      3.0   0.000000       NaN
4      4.0   0.000000       NaN
..     ...        ...       ...
355  355.0   0.000000  0.000000
356  356.0   0.447368  0.044737
357  357.0   0.500000  0.094737
358  358.0   0.526316  0.147368
359  359.0   0.500000  0.197368

但是存在一个问题:我丢失了0到9之间的数值,这对我来说很重要。
因此,我的脚本必须使用351、352、353、354、355度等角度来计算0、1、2、3...度的平均值。

我期望输出:

    degree      value                     smoothed
0      0.0   0.526316  mean value of 351-0 degrees
1      1.0   0.000000  mean value of 352-1 degrees
2      2.0   0.000000  mean value of 353-2 degrees
3      3.0   0.000000  mean value of 354-3 degrees
4      4.0   0.000000  mean value of 355-4 degrees
................
and so on    

如何做到? 谢谢.


1
请发布您期望的输出。 - Mayank Porwal
2个回答

3

在进行滚动平均之前,您可以将最后的9个值添加到数据框的开头,以便0获得351-0的平均值,1获得352-1的平均值,以此类推:

df1 = df[-9:].append(df)
df['smoothed'] = df1['value'].rolling(window=10).mean().dropna()

    degree     value  smoothed
0      0.0  0.421053  0.457895
1      1.0  0.473684  0.450000
2      2.0  0.473684  0.450000
3      3.0  0.473684  0.450000
4      4.0  0.500000  0.452632
5      5.0  0.500000  0.457895
6      6.0  0.500000  0.463158
7      7.0  0.473684  0.465789
8      8.0  0.473684  0.471053
9      9.0  0.473684  0.476316
10    10.0  0.394737  0.473684
....
....

1
可以使用索引而无需在数据框前面添加。df['smoothed'] = df.loc[np.hstack((df.index.values[-9:], df.index.values)), 'value'].rolling(window=10).mean()[9:] - ai2ys

1
似乎你需要一个循环滑动窗口,例如,120会从101到120平均计算,但0会从351到0平均计算。不幸的是,Pandas不支持此功能,但你可以将值转换以强制执行此操作。只需将值351复制到-9,352复制到-8,依此类推,然后进行滚动平均(显然仅保留正角度)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接