Use groupby with join
aggregation and add_prefix to rename index:
df.groupby('var1')['var2'].agg(', '.join).add_prefix('list_')
[out]
var1
list_0 a, b, c, d, z
list_1 t, a
list_2 p
list_60 c
Name: var2, dtype: object
or for python lists use list
aggregation:
df.groupby('var1')['var2'].agg(list).add_prefix('list_')
[out]
var1
list_0 [a, b, c, d, z]
list_1 [t, a]
list_2 [p]
list_60 [c]
Name: var2, dtype: object
Update
I think I see what you're trying to achieve, my strong advice would be to use a python dict
instead of "independent lits" - with the keys being list_0
, list_1
, etc...
Example
d = df.groupby('var1')['var2'].agg(list).add_prefix('list_').to_dict()
print(d['list_0'])
[out]
['a', 'b', 'c', 'd', 'z']
If you absolutely insist on independent lists, then use the globals()
object, and update with a for
loop (for the avoidance of doubt, I do not recommend this method - check out this question for more info):
s = df.groupby('var1')['var2'].agg(list).add_prefix('list_')
for var, lst in s.iteritems():
globals()[var] = lst
You should now have independent lists with associated variable names.