MenorcanOrange
MenorcanOrange

Reputation: 2825

Pandas dataframe grouping values

I have a pandas dataframe like this,

dd = pd.DataFrame(
{'name': ['abc','bcd','abc'],
 'seconds': [75,77,90],
})

enter image description here

I need to combine the seconds column into a single list for rows with same name.

I am able to do this using for loop,

names= list(set(dd['name']))
counter=[]
for a in names:
    counter.append(list(dd[dd['name'] == a]['seconds']))
end
seconds_list = pd.DataFrame(
{'name': names,
'seconds': counter,
})

Output:

enter image description here

But this takes a lot of time on a big dataframe. Any simple way to achieve this without a for loop?

Thanks!

Upvotes: 1

Views: 82

Answers (2)

Scott Boston
Scott Boston

Reputation: 153460

Use groupby, agg, and tolist:

 dd.groupby('name')['seconds'].agg(lambda x: x.tolist()).reset_index(name='seconds')

Output:

  name   seconds
0  abc  [75, 90]
1  bcd      [77]

Upvotes: 1

jezrael
jezrael

Reputation: 862641

Use groupby with apply list:

df = dd.groupby('name')['seconds'].apply(list).reset_index()
print (df)

  name   seconds
0  abc  [75, 90]
1  bcd      [77]

Upvotes: 2

Related Questions