How to compile all lists in a column into one unique list

后端 未结 4 1265
有刺的猬
有刺的猬 2020-12-10 15:28

I have a pandas dataframe as below:

How can I combine all the lists (in the \'val\' column) into a unique list (set), e.g. [val1, val2, val33, val9, v

4条回答
  •  感情败类
    2020-12-10 15:56

    You can use str.concat followed by some string manipulations to obtain the desired list.

    In [60]: import re
        ...: from collections import OrderedDict
    
    In [62]: s = df['val'].str.cat()
    
    In [63]: L = re.sub('[[]|[]]',' ', s).strip().replace("  ",',').split(',')
    
    In [64]: list(OrderedDict.fromkeys(L))
    Out[64]: ['val1', 'val2', 'val33', 'val9', 'val6', 'val7']
    

提交回复
热议问题