0

I have this dataframe :

 A     B      C
 1     14    100
 1     15    101
 1     16    102
 2     17    103
 2     18    104
 3     19    105
 3     20    106
...   ...    ...
 n     

and I would like this output for any number up to n for the whole dataframe :

l1 = [14, 15, 16] (the 1 in A column)
l2 = [17,18]  (the 2 in A column)
l3 = [19,20] (the 3 in A column)

Could you please help me? Tanks !

1 Answer 1

5

Try:

l1, l2, l3 = df.groupby('A')['B'].apply(list).values

Unpacking will only work only if there are three unique values in df['A'], so you probably wouldn't want to unpack the values and just index the resulting DataFrame from:

>>> df.groupby('A')['B'].apply(list)
A
1    [14, 15, 16]
2        [17, 18]
3        [19, 20]
Sign up to request clarification or add additional context in comments.

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.