Reputation: 34673
I have a rowset that looks like this:
defaultdict(<type 'dict'>,
{
u'row1': {u'column1': 33, u'column2': 55, u'column3': 23},
u'row2': {u'column1': 32, u'column2': 32, u'column3': 17},
u'row3': {u'column1': 31, u'column2': 87, u'column3': 18}
})
I want to be able to easily get the sum of column1, column2, column3. It would be great if I could do this for any number of columns, receiving the result in a hash map that looks like columnName => columnSum
. As you might guess, its not possible for me to obtain the summed values from the database in first place, thus the reason to ask the question.
Upvotes: 3
Views: 3293
Reputation: 3261
Here's another answer, if I may suggest a solution. First put your data in a Matrix. Then, multiply the matrix by a vector of ones.
import numpy as np
A = np.random.normal(size = (3,3))
Now to get the sum of columns, simply use the dot product.
np.dot(A, np.ones(3))
To stack the rows rather than the columns, simply transpose the matrix.
np.dot(A.T, np.ones(3))
Upvotes: 0
Reputation: 133554
>>> from collections import defaultdict
>>> x = defaultdict(dict,
{
u'row1': {u'column1': 33, u'column2': 55, u'column3': 23},
u'row2': {u'column1': 32, u'column2': 32, u'column3': 17},
u'row3': {u'column1': 31, u'column2': 87, u'column3': 18}
})
>>> sums = defaultdict(int)
>>> for row in x.itervalues():
for column, val in row.iteritems():
sums[column] += val
>>> sums
defaultdict(<type 'int'>, {u'column1': 96, u'column3': 58, u'column2': 174})
Ooh a much better way!
>>> from collections import Counter
>>> sums = Counter()
>>> for row in x.values():
sums.update(row)
>>> sums
Counter({u'column2': 174, u'column1': 96, u'column3': 58})
Upvotes: 7
Reputation: 39548
Nested generators + list comprehension does the trick:
>>> foo
defaultdict(<type 'dict'>, {u'row1': {u'column1': 33, u'column3': 23, u'column2': 55}, u'row2': {u'column1': 32, u'column3': 17, u'column2': 32}, u'row3': {u'column1': 31, u'column3': 18, u'column2': 87}})
>>> dict(zip(foo.values()[0].keys(), [sum(j[k] for j in (i.values() for _,i in foo.items())) for k in range(3)]))
{u'column1': 96, u'column3': 58, u'column2': 174}
Upvotes: 2
Reputation: 7380
not the most pythonic but here:
for val in defaultdict.values() :
sum1 += val['column1']
sum2 += val['column2']
sum3 += val['column3']
final_dict = {'column1' : sum1,'column2' : sum2,'column3' : sum3 }
Upvotes: 0