Reputation: 31
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 !
Upvotes: 0
Views: 188
Reputation: 376
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]
Upvotes: 5