I have a list of values in python who change between positive and negative values, and I want to work with the average of those values. However, I need initialize a counter when the number was positive and end the counting before it becomes positive again, on the last negative number. Then, take the average of this counting.
Here is my sample data:
0; 2.3360; 0.4675
1; 1.7439; 0.4174
2; 1.3766; 0.3673
3; 1.3766; 0.1719
4; 1.4002; 0.1719
5; 1.5687; 0.1719
6; 2.2238; -0.6552
7; 1.6181; -0.6552
8; 2.2797; -0.6552
9; 2.9562; -0.6552
10; 3.4301; -0.6552
11; 3.7597; -0.6552
12; 4.0999; -0.6552
13; 4.6294; -0.6552
14; 4.4860; -0.6552
15; 4.4504; 0.0356
16; 4.3090; 0.1414
17; 3.9967; 0.1556
18; 3.8269; 0.1698
19; 3.4952; 0.1978
20; 3.2694; 0.1307
21; 3.2059; 0.0635
22; 3.1428; 0.0631
23; 3.0802; 0.0626
24; 2.9562; -0.0619
25; 2.8950; -0.0612
25; 2.8950; -0.0612
26; 2.4214; -0.1155
27; 2.2517; -0.1697
28; 2.0055; -0.1900
29; 1.7952; -0.1835
30; 1.7952; 0.1835
In this case, I would need make the average from the value on the 0 position until the value on the 14 position, after, from the 15 until the 29, and from the 30 until the end.
However, this average is relative from the values on the second column, vazmed[1], but the range for this average, len(n), will variate with the third column, vazmed[2]
I'm tryna do this with an auxiliary variable, but, without success.
Here is what i did until now:
arq = open('vazdif.out', 'rt')
vazmed1 = []
vazmed2 = []
i = 0
for row in arq:
field = row.split(';')
vaz1 = float(field[1])
vaz2 = float(field[2])
vazmed1.append(vaz1)
vazmed2.append(vaz2)
i = i+1
n = len(vazmed1)
m = sum(vazmed1)
aux = 0
for actual in vazmed2:
if actual < 0:
aux = 1
if actual >= 0 and aux == 1:
aux = 0
if aux == 1:
average = m/n
print(average)
If anybody could help me, and also explain where am i going wrong, I will be grateful.