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