You can use groupby:
s = df.groupby('three')['two'].apply(list)
print (s)
three
1 [2, 1, 2]
2 [1, 2]
Name: two, dtype: object
a = s.loc[1]
b = s.loc[2]
print (a)
[2, 1, 2]
print (b)
[1, 2]
If you need nested lists:
L = df.groupby('three')['two'].apply(list).tolist()
print (L)
[[2, 1, 2], [1, 2]]
Other possible solutions:
L = [list(x) for i, x in df.groupby('three')['two']]
print (L)
[[2, 1, 2], [1, 2]]
L = [x.tolist() for i, x in tuple(df.groupby('three')['two'])]
print (L)
[[2, 1, 2], [1, 2]]
source
share