简体   繁体   中英

Weighting a Rolling Pandas Dataframe

So I have a dataframe:

import pandas as pd

values = [4,6,4,8,13,20,2,4,3,6,5,8,2,5,7, 20, 5, 6 , 7, 9, 20 , 5, 3, 5, 20, 4, 10, 2]

example = pd.DataFrame({'Values' : values})

example

And I want to do a rolling calculation on a moving window of these where the values are weighted. Specifically a window of 10 with ai/10 weighting. So the first 9 values would be blank then the 10th:

(1/10 x 4) + (2/10 x 6) + (3/10 x 4) + (4/10 x 8) + (5/10 x 13) + (6/10 x 20) + (7/10 x 2) + (8/10 x 4) + (9/10 x 3) + (10/10 x 6)

then the 11th value would be

(1/10 x 6) + (2/10 x 4) + (3/10 x 8) + (4/10 x 13) + (5/10 x 20) + (6/10 x 2) + (7/10 x 4) + (8/10 x 3) + (9/10 x 6) + (10/10 x 5)

etc.

I can do the rolling bit using the the.rolling() function but I can't find out how to weight these individually. Sorry this is probably quite a basic question.

IIUC, create a weighted array:

WINDOWSIZE = 10
w = np.arange(1, WINDOWSIZE+1)
out = df.rolling(WINDOWSIZE).apply(lambda x: sum(w/WINDOWSIZE * x))
print(out)

# Output:
    Values
0      NaN
1      NaN
2      NaN
3      NaN
4      NaN
5      NaN
6      NaN
7      NaN
8      NaN
9     37.8  # (1/10 x 4) + (2/10 x 6) + (3/10 x 4) + (4/10 x 8) + (5/10 x 13) + (6/10 x 20) + (7/10 x 2) + (8/10 x 4) + (9/10 x 3) + (10/10 x 6)
10    35.8  # (1/10 x 6) + (2/10 x 4) + (3/10 x 8) + (4/10 x 13) + (5/10 x 20) + (6/10 x 2) + (7/10 x 4) + (8/10 x 3) + (9/10 x 6) + (10/10 x 5)
11    36.7
12    31.4
13    29.3
14    29.5
15    43.3
16    42.1
17    41.6
18    41.9
19    43.8
20    56.4
21    52.5
22    46.9
23    43.2
24    54.5
25    48.5
26    50.1
27    43.2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM