Reputation: 2825
I have a pandas dataframe like this,
dd = pd.DataFrame(
{'name': ['abc','bcd','abc'],
'seconds': [75,77,90],
})
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:
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
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