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

后端 未结 4 1263
有刺的猬
有刺的猬 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:54

    One way would be to extract those elements into an array using np.hstack and then using np.unique to give us an array of such unique elements, like so -

    np.unique(np.hstack(df.val))
    

    If you want a list as output, append with .tolist() -

    np.unique(np.hstack(df.val)).tolist()
    

提交回复
热议问题