3

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.

4 Answers 4

7
>>> 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}) 
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for explicit better than implicit and output from console.
2

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}

1 Comment

Return the result in a hash map?
0

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 }

Comments

0

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.