1

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!

2 Answers 2

2

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]
Sign up to request clarification or add additional context in comments.

Comments

1

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.