Python - Group by and sum a list of tuples

python group list of tuples by first element
tuple matching python example
tuple assignment python
tuple duplication python

Given the following list:

[
    ('A', '', Decimal('4.0000000000'), 1330, datetime.datetime(2012, 6, 8, 0, 0)),
    ('B', '', Decimal('31.0000000000'), 1330, datetime.datetime(2012, 6, 4, 0, 0)),
    ('AA', 'C', Decimal('31.0000000000'), 1330, datetime.datetime(2012, 5, 31, 0, 0)),
    ('B', '', Decimal('7.0000000000'), 1330, datetime.datetime(2012, 5, 24, 0, 0)),
    ('A', '', Decimal('21.0000000000'), 1330, datetime.datetime(2012, 5, 14, 0, 0))
]

I would like to group these by the first, second, fourth and fifth columns in the tuple and sum the 3rd. For this example I'll name the columns as col1, col2, col3, col4, col5.

In SQL I would do something like this:

select col1, col2, sum(col3), col4, col5 from my table
group by col1, col2, col4, col5

Is there a "cool" way to do this or is it all a manual loop?

>>> [(x[0:2] + (sum(z[2] for z in y),) + x[2:5]) for (x, y) in
      itertools.groupby(sorted(L, key=operator.itemgetter(0, 1, 3, 4)),
      key=operator.itemgetter(0, 1, 3, 4))]
[
  ('A', '', Decimal('21.0000000000'), 1330, datetime.datetime(2012, 5, 14, 0, 0)),
  ('A', '', Decimal('4.0000000000'), 1330, datetime.datetime(2012, 6, 8, 0, 0)),
  ('AA', 'C', Decimal('31.0000000000'), 1330, datetime.datetime(2012, 5, 31, 0, 0)),
  ('B', '', Decimal('7.0000000000'), 1330, datetime.datetime(2012, 5, 24, 0, 0)),
  ('B', '', Decimal('31.0000000000'), 1330, datetime.datetime(2012, 6, 4, 0, 0))
]

(NOTE: output reformatted)

Groupby and count/sum with list of tuples in python?, groupby is a function from itertools , but it's not quite what you want. Instead, lets import defaultdict from collections from collections import  Python – Combinations of sum with tuples in tuple list Sometimes, while working with data, we can have a problem in which we need to perform tuple addition among all the tuples in list. This can have application in many domains.

You want itertools.groupby.

Note that groupby expects the input to be sorted, so you may need to do that before hand:

keyfunc = lambda t: (t[0], t[1], t[3], t[4])
data.sort(key=keyfunc)
for key, rows in itertools.groupby(data, keyfunc):
    print key, sum(r[2] for r in rows)

Python, Aggregate values by tuple keys. # using groupby() + map() + itemgetter() + sum(). from itertools import groupby. from operator import itemgetter. # initialize list. Python – Sum of tuple elements Sometimes, while programming, we have a problem in which we might need to perform summation among tuple elements. This is an essential utility as we come across summation operation many times and tuples are immutable and hence required to be dealt with.

If you find yourself doing this a lot with large datasets, you might want to look at the pandas library, which has lots of nice facilities for doing this kind of thing.

Python, Given a list of tuples, the task is to group the tuples by matching the second of sum with tuples in tuple list · Python | Swap tuple elements in list of tuples  Python Exercises, Practice and Solution: Write a Python program to find a tuple, the smallest second index value from a list of tuples.

List of tuples: group by first element of tuple; sum up the second , Summary of the methods given in the comments to the question. data = {{0, 3}, {1, 1}, {1, 2}, {2, 3}, {2, 4}};. swish (requires V10 or later) Python Tuples. Python provides another type that is an ordered collection of objects, called a tuple. Pronunciation varies depending on whom you ask. Some pronounce it as though it were spelled “too-ple” (rhyming with “Mott the Hoople”), and others as though it were spelled “tup-ple” (rhyming with “supple”).

Group By: split-apply-combine, Filter out data based on the group sum or mean. A Python function, to be called on each of the axis labels. A list or A list of any of the above things. in aggregate(self, func_or_funcs, *args, **kwargs) 849 # but not the class list / tuple itself. Python | Group by matching second tuple value in list of tuples. Given a list of tuples, the task is to group the tuples by matching the second element in the tuples. We can achieve this using dictionary by checking the second element in each tuple.

5. Data Structures, Return the index in the list of the first item whose value is x. If the expression is a tuple (e.g. the (x, y) in the previous example), it must be  Python | Summation of list as tuple attribute Many times, while dealing with containers in any language we come across lists of tuples in different forms, tuples in themselves can have sometimes more than native datatypes and can have list as their attributes.

Comments
  • operator.itemgetter(0,1,3,4)