Grouping the same name in lists in python

Question:

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.

Asked By: Aman Baweja

||

Answers:

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
Answered By: Abbas
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.