I have a Pandas dataframe like this:

```
name number
A   0.8466
A   0.8463
A   0.8482
A   0.8455
A   0.8423
A   0.8405
A   0.842
A   0.8453
A   0.8419
A   0.8394
A   0.8376
A   0.8368
A   0.8388
A   0.8392
A   0.8409
A   0.8415
A   0.8424
A   0.8425
A   0.8433
A   0.8412
A   0.8397

```

I would like to count how many times an `upper` and how many times a `lower` condition is met in a sliding window of 3 `number` values. I have a working code:

```
df['Upper'] = 0
df['Lower'] = 0
for i in range(len(df)):
    if i < 1:
        df['Upper'].iloc[i] = 0
        df['Lower'].iloc[i] = 0
    else:
        chip_sum_r = 0
        chip_sum_s = 0
        for j in range(3):
            if df['name'].iloc[i-j] == df['name'].iloc[i]:
                if df['number'].iloc[i-j] <= df['number'].iloc[i]*1.003 and df['number'].iloc[i-j] >   df['number'].iloc[i]:
                    chip_sum_r += 1
                if df['number'].iloc[i-j] >= df['number'].iloc[i]*0.997 and df['number'].iloc[i-j] < df['number'].iloc[i]:
                   chip_sum_s += 1
        df['Upper'].iloc[i] = chip_sum_r
        df['Lower'].iloc[i] = chip_sum_s

```

In the end I get:

```
    name  number   Upper  Lower
0     A  0.8466      0      0
1     A  0.8463      1      0
2     A  0.8482      0      2
3     A  0.8455      1      0
4     A  0.8423      0      0
5     A  0.8405      1      0
6     A  0.8420      1      1
7     A  0.8453      0      0
8     A  0.8419      1      0
9     A  0.8394      1      0
10    A  0.8376      1      0
11    A  0.8368      1      0
12    A  0.8388      0      2
13    A  0.8392      0      2
14    A  0.8409      0      2
15    A  0.8415      0      2
16    A  0.8424      0      2
17    A  0.8425      0      2
18    A  0.8433      0      2
19    A  0.8412      2      0
20    A  0.8397      1      0

```

However this is very slow for large number of data points. Is there a way to speed this up, I am not sure whether a vectorized approach would work here?