Reputation: 391
If I had data such as:
data = [[3, 2014], [4, 2014], [6, 2013], [6,2013]] etc...
What is the best way to calculate the sum by year in python?
Upvotes: 1
Views: 1553
Reputation: 36337
There's a specific python standard library class for that, Counter
:
from collections import Counter
from operator import add
counters = [Counter({row[1]:row[0]}) for row in data]
result = reduce(add, counters)
your result is a dict-behaving object:
{2013: 12, 2014: 7}
Upvotes: 1
Reputation: 180391
I would use a dict if you need both the year and sum:
from collections import defaultdict
data = [[3, 2014], [4, 2014], [6, 2013], [6,2013]]
d = defaultdict(int)
for v, k in data:
d[k] += v
print(d)
Prints:
defaultdict(<type 'int'>, {2013: 12, 2014: 7})
Upvotes: 3
Reputation: 1433
As reported by DSM, using pandas and grouby it seems easy:
import pandas as pd
data = [[3, 2014], [4, 2014], [6, 2013], [6,2013]]
df = pd.DataFrame(data, columns=['value', 'year'])
df.groupby(['year']).sum()
which returns:
value
year
2013 12
2014 7
It nice because you can easy get more information like mean, median, std etc..
df.groupby(['year']).mean()
df.groupby(['year']).median()
df.groupby(['year']).std()
Upvotes: 1
Reputation: 5696
You can use counter()
and +=
.
import collections
data = [[3, 2014], [4, 2014], [6, 2013], [6,2013]]
c = collections.Counter()
for i, j in data:
c += collections.Counter({j: i})
print(c)
A Counter is a dict subclass for counting hashable objects. It is an unordered collection where elements are stored as dictionary keys and their counts are stored as dictionary values.
You can add Counters, for example:
a = collections.Counter(a=1, b=2)
b = collections.Counter(a=3, c=3)
print(a+b)
prints Counter({'a': 4, 'c': 3, 'b': 2})
.
Upvotes: 0
Reputation: 15423
Not sure if I understand the question. Here might be a simple answer without added modules.
dic = {}
for dat, year in data:
if year not in dic:
dic[year] = dat
else:
dic[year] += dat
or if you prefer
dic = {}
for dat, year in data:
dic[year] = dat if year not in dic else dic[year] + dat
Upvotes: 1