2

Input:
Dataframe

col1    col2
item1   10
item1   20
item1   25
item2   56
item2   36
item3   1

Output:
List of List

[[10,20,25],[56,36],[1]]

2 Answers 2

2

Create Series of lists by groupby with GroupBy.apply and last convert it to list:

L = df.groupby('col1')['col2'].apply(list).tolist()
print (L)
[[10, 20, 25], [56, 36], [1]]
Sign up to request clarification or add additional context in comments.

Comments

1

You can GroupBy col1 and extract lists from each group of on col2:

df.groupby('col1').col2.apply(list).values.tolist()
# [[10, 20, 25], [56, 36], [1]]

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.