Q:

How to find mean difference within a rolling window in pandas dataframe? 

I have the following 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

I want the mean column which gets the mean difference of the preceding 7 values in the value column, e.g. for index 7, I want: ((9-8) + (8-7) + (7-6) + (6-5) + (5-4) + (4-3))/6.  
I don't now what to do after df['value].rolling(7).  Any help would be highly appreciated. 

A:

Use Series.diff with Series.shift and rolling by 6 with 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

If rolling 7 is necessary:
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

