7

I am using the Pandas rolling window tool on a one-column dataframe whose index is in datetime form.

I would like to compute, for each window, the difference between the first value and the last value of said window. How do I refer to the relative index when giving a lambda function? (in the brackets below)

df2 = df.rolling('3s').apply(...)
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
Pandora
  • 203
  • 1
  • 3
  • 7

1 Answers1

12

IIUC:

In [93]: df = pd.DataFrame(np.random.randint(10,size=(9, 3)))

In [94]: df
Out[94]:
   0  1  2
0  7  4  5
1  9  9  3
2  1  7  6
3  0  9  2
4  2  3  7
5  6  7  1
6  1  0  1
7  8  4  7
8  0  0  9

In [95]: df.rolling(window=3).apply(lambda x: x[0]-x[-1])
Out[95]:
     0    1    2
0  NaN  NaN  NaN
1  NaN  NaN  NaN
2  6.0 -3.0 -1.0
3  9.0  0.0  1.0
4 -1.0  4.0 -1.0
5 -6.0  2.0  1.0
6  1.0  3.0  6.0
7 -2.0  3.0 -6.0
8  1.0  0.0 -8.0
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
  • and what if you wanted the first and last row of each window as well? tried the apply approach, it works but simply does not scale well. – Abhishek Malik Mar 31 '20 at 22:45
  • 4
    Is there a way to do this without apply? apply is insanely slow – rindis Apr 09 '21 at 15:33
  • I was getting `KeyError: -1` with pandas 1.5.2. Using `iloc` fixed the issue: `df.rolling(window=3).apply(lambda x: x.iloc[0]-x.iloc[-1])`. – AXO Jan 23 '23 at 14:35