HappyPy
HappyPy

Reputation: 10697

create tuples from pandas groupby

df = pd.DataFrame({'col1':['A','A','B'], 'col2':[1,2,1]})

I'd like to group the results into tuples inside a list:

[ ('A', [1,2]), ('B',[1]) ]

I thought list(df.groupby(by='col1')['col2']) would be sufficient but the second element of the tuple is a Series.

df.groupby(by='col1')['col2'].apply(lambda x: x.values.tolist()) also does not quite do what I intend.

Upvotes: 1

Views: 1731

Answers (3)

Ch3steR
Ch3steR

Reputation: 20669

You can use GroupBy.agg to list, then use df.to_records and convert it to list.

df.groupby('col1').agg(list).to_records().tolist()
# [('A', [1, 2]), ('B', [1])]

Upvotes: 2

mhhabib
mhhabib

Reputation: 3121

Make a list zip according to your groupby value index

import pandas as pd
df = pd.DataFrame({'col1':['A','A','B'], 'col2':[1,2,1]})
dff= df.groupby(by='col1')['col2'].apply(lambda x: x.values.tolist())
df_f= list(zip(dff.index, dff))
df_f

Upvotes: 1

sammywemmy
sammywemmy

Reputation: 28709

You could add the agg function with list and then zip the final result :

result = df.groupby(by="col1")["col2"].agg(list)
list(zip(result.index, result))
[('A', [1, 2]), ('B', [1])]

Upvotes: 1

Related Questions