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

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

    Convert that column into a DataFrame with .apply(pd.Series). If you stack the columns, you can call the unique method on the returned Series.

    df
    Out[123]: 
                val
    0      [v1, v2]
    1      [v3, v2]
    2  [v4, v3, v2]
    

    df['val'].apply(pd.Series).stack().unique()
    Out[124]: array(['v1', 'v2', 'v3', 'v4'], dtype=object)
    

提交回复
热议问题