Create multiple lists from pandas df with conditional logic [duplicate]

筅森魡賤 提交于 2020-03-16 08:08:29

问题


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, my output would look something like this:

list_0: a, b, c, d, z
list_1: t, a
list_2: p
list_60: c

Currently I'm trying to work out a loop to do this, something like:

for i in range(df.var2.max()):
    var2_i = (x for x in df.var1.to_list())

Though the lists don't seem to be iteratively created here. Perhaps there's a better way to accomplish my goal?


回答1:


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.



来源:https://stackoverflow.com/questions/60643396/create-multiple-lists-from-pandas-df-with-conditional-logic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!