如何在熊猫数据框的滚动窗口内找到均值差异?

时间:2019-04-20 08:13:05

标签: python-3.x pandas dataframe

我有以下数据框:

    value  year
0       9  2011
1       8  2011
2       7  2011
3       6  2011
4       5  2011
5       4  2011
6       3  2011
7       2  2011
8       8  2011
9       2  2011
10      0  2011
11      5  2011

我想要平均值列,该列获取值列中前7个值的均值差,例如对于索引7,我想要:((9-8) + (8-7) + (7-6) + (6-5) + (5-4) + (4-3))/6

df['value].rolling(7)之后,我现在不该做什么。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

Series.diffSeries.shift一起使用,并与6一起滚动mean

print (((9-8) + (8-7) + (7-6) + (6-5) + (5-4) + (4-3))/6)
1.0
print (((8-7) + (7-6) + (6-5) + (5-4) + (4-3) + (3-2))/6)
1.0
print (((7-6) + (6-5) + (5-4) + (4-3) + (3-2) + (2-8))/6)
-0.16666666666666666
print (((6-5) + (5-4) + (4-3) + (3-2) + (2-8) + (8-2))/6)
0.6666666666666666

df['new'] = df['value'].diff(-1).shift().rolling(6).mean()
print (df)
    value  year       new
0       9  2011       NaN
1       8  2011       NaN
2       7  2011       NaN
3       6  2011       NaN
4       5  2011       NaN
5       4  2011       NaN
6       3  2011  1.000000
7       2  2011  1.000000
8       8  2011 -0.166667
9       2  2011  0.666667
10      0  2011  0.833333
11      5  2011 -0.166667

如果需要滚动7

print (((9-8) + (8-7) + (7-6) + (6-5) + (5-4) + (4-3) + (3-2))/7)
1.0
print (((8-7) + (7-6) + (6-5) + (5-4) + (4-3) + (3-2) + (2-8))/7)
0.0
print (((7-6) + (6-5) + (5-4) + (4-3) + (3-2) + (2-8  + (8-2)))/7)
0.7142857142857143
print (((6-5) + (5-4) + (4-3) + (3-2) + (2-8) + (8-2) + (2+0))/7)
0.8571428571428571

df['new'] = df['value'].diff(-1).shift().rolling(7).mean()
print (df)
    value  year       new
0       9  2011       NaN
1       8  2011       NaN
2       7  2011       NaN
3       6  2011       NaN
4       5  2011       NaN
5       4  2011       NaN
6       3  2011       NaN
7       2  2011  1.000000
8       8  2011  0.000000
9       2  2011  0.714286
10      0  2011  0.857143
11      5  2011  0.000000