Python 计算360度圆内的移动平均数

Python 计算360度圆内的移动平均数,python,pandas,Python,Pandas,这是我的数据 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 ...........

这是我的数据

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。它们对我很重要。
所以,我的脚本必须使用度数351352353354355等等来计算度数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    

怎么做?谢谢。

从逻辑上讲,您似乎在寻找一个圆形滑动窗口,例如,其中120表示从101到120的平均值,而0表示从351到0的平均值


不幸的是,Pandas不支持这一点,但您可以转换这些值来强制它这样做。只需将值351复制到-9,352复制到-8,依此类推,然后取一个滚动平均值(显然只保留正角度)

在取
滚动平均值之前,您可以将最后的
9
值预先添加到数据帧中,以便
0
得到
351-0
的平均值,
1
获取
352-1
的平均值,依此类推:

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


请发布您的预期输出。无需使用索引预先附加数据帧即可<代码>df['smooted']=df.loc[np.hstack((df.index.values[-9:],df.index.values)),'value'].rolling(window=10.mean()[9:]
    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
....
....