Reputation: 522
Actually I'm calculating throughput given certain window size. However, I don't know how to accumulate the values by window. For instance:
time = [0.9, 1.1, 1.2, 2.1, 2.3, 2.6]
value = [1, 2, 3, 4, 5, 6]
After window size with 1 is applied, I should get
new_value = [1, 5, 15]
I've thought of using indexes of data frame but not sure how I can realize it since I'm new to python.
Upvotes: 2
Views: 201
Reputation: 54293
You could use a Counter
:
time = [0.9, 1.1, 1.2, 2.1, 2.3, 2.6]
value = [1, 2, 3, 4, 5, 6]
from collections import Counter
counter = Counter()
for t,v in zip(time, value):
counter[int(t)] += v
print(sorted(counter.items()))
# [(0, 1), (1, 5), (2, 15)]
Upvotes: 1
Reputation: 46583
You could use itertools.groupby
with a custom grouping function
from itertools import groupby
def f(time, values, dt=1):
vit = iter(values)
return [sum(v for _, v in zip(g, vit)) for _, g in groupby(time, lambda x: x // dt)]
In [14]: f([0.9, 1.1, 1.2, 2.1, 2.3, 2.6], [1, 2, 3, 4, 5, 6])
Out[14]: [1, 5, 15]
In [15]: f([0.9, 1.1, 1.2, 2.1, 2.3, 2.6], [1, 2, 3, 4, 5, 6], dt=2)
Out[15]: [6, 15]
Note that for the window of size 1 you could simply use groupby(time, int)
.
Upvotes: 3