Suppose I have the following df data frame:
date symbol_a symbol_b ratio
0 2017/01/01 AAAA AA 10
1 2017/01/02 AAAA AA 20
2 2017/01/03 AAAA AA 30
3 2017/01/04 AAAA AA 10
4 2017/01/05 AAAA AA 10
5 2017/01/06 AAAA AA 10
6 2017/01/01 BBBB BB 10
7 2017/01/02 BBBB BB 20
8 2017/01/03 BBBB BB 30
9 2017/01/04 BBBB BB 10
10 2017/01/01 CCCC CC 10
11 2017/01/02 CCCC CC 20
12 2017/01/03 CCCC CC 30
13 2017/01/04 CCCC CC 10
14 2017/01/05 CCCC CC 10
15 2017/01/06 CCCC CC 5
I'm interested in the average values of the relation column (this comes from the previous data frame, which had two additional columns value_a value_b and ratio = value_a / value_b, more or less). I would like to do the following:
take averages of character a (or _b is actually the same) every n elements. Suppose n = 3.
Usually I would do something like:
df.groupby(['symbol_a','symbol_b']).mean()
However, I would like to receive intermediate funds every 3 days (the actual period of time is obviously much longer, and I will need every 5).
At first I thought that I would always have the same number of characters divisible by n, so I tried something like:
df.groupby([df.index/n, 'symbol_a', 'symbol_b']).mean().reset_index()
, n. , , , , n : , , symbol_a "BBBB" (). , , , .
, -, n , n, n , , ( number_of_symbols <n ).
, (, n = 3):
symbol_a symbol_b 3_mean_ratio
0 AAAA AA 20
1 AAAA AA 10
2 BBBB BB 20
4 BBBB BB 10
5 CCCC CC 20
6 CCCC CC 8.33
- ? , .
: . , , - n-days ratio . , , , . " " . , , . - , :
date symbol_a symbol_b ratio n-days-ratio
0 2017/01/01 AAAA AA 10 20
1 2017/01/02 AAAA AA 20 20
2 2017/01/03 AAAA AA 30 20
3 2017/01/04 AAAA AA 10 10
4 2017/01/05 AAAA AA 10 10
5 2017/01/06 AAAA AA 10 10
6 2017/01/01 BBBB BB 10 20
7 2017/01/02 BBBB BB 20 20
8 2017/01/03 BBBB BB 30 20
9 2017/01/04 BBBB BB 10 10
10 2017/01/01 CCCC CC 10 20
11 2017/01/02 CCCC CC 20 20
12 2017/01/03 CCCC CC 30 20
13 2017/01/04 CCCC CC 10 8.3
14 2017/01/05 CCCC CC 10 8.3
15 2017/01/06 CCCC CC 5 8.3