1

I am new to python, so would like your help on the matter. I have a list of tuples. I want to group these lists on the basis of the first element of the list, which may repeat. Also, I would like to sum the 2nd and 3rd element of the list for repeating elements.

Sample Input:

[("henry", 10, 20), ("alex", 25, 40), ("henry", 13, 21)]

Sample output:

[("henry", 23, 41), ("alex", 25, 40)]

I have tried the groupby method in python, but it works only on sorted lists.

Bijoy Thangaraj
  • 5,434
  • 4
  • 43
  • 70
Aman Baweja
  • 35
  • 1
  • 1
  • 4
  • You need to use [itertools.groupby](https://docs.python.org/2/library/itertools.html#itertools.groupby). A similar kind of problem is already addressed and solved using `itertools.groupby` in this StackOverflow question: http://stackoverflow.com/questions/2249036/grouping-python-tuple-list Please make sure to look for existing questions that might be similar before posting a duplicate. – Munim May 20 '16 at 06:52
  • @Md.AbdulMunim: `groupby` requires the input to be sorted. – Martijn Pieters May 20 '16 at 06:56

1 Answers1

3

IIUC here is a solution using pandas:

lst = [['henry', 10, 20], ['henry', 23, 42], ['alex', 25, 40]]
import pandas as pd
#Create a dataframe using the list
df = pd.DataFrame(lst)
#Name the columns
df.columns = ['name', 'val1', 'val2']
#Groupby name and sum it.
df.groupby('name').sum()
#Will produce this result
       val1  val2
name
alex     25    40
henry    33    62

#To access the rows and columns following is the example
dfg = df.groupby('name').sum()
#Row, Column
dfg.iloc[0][0]
#Will get 25 & likewise can get any other values
25 
#Another way to get
dfg.loc["alex"][0]
#Will get 25 & likewise can get any other values
25
Abbas
  • 3,872
  • 6
  • 36
  • 63