I have a df that looks like this:
var1 var2 var3
0 a 1
0 b 7
0 c 5
0 d 4
0 z 8
1 t 9
1 a 2
2 p 3
.. .. ..
60 c 3
I'm attempting to create lists of each set of values from var2 that correspond to a given value from var1. So, the result would look something like this:
list_0: a, b, c, d, z
list_1: t, a
list_2: p
list_60: c
My desired behavior is such that I would be able to do print(list_0)
and have returned the values of var2
associated with var1 == 0
.
Currently I'm trying to work out a loop to do this, something like:
for i in range(df['var1'].max()):
list['list_'+str(i)] = []
stops_i.append(x for x in df['var2'])
Though the lists don't seem to be iteratively created here. Perhaps there's a better way to accomplish my goal?
I have also tried using groupby as was suggested in another SO post, though that returns a groupby object which I would then need to further break out into individual lists and does not behave in the way I'd like.