Search code examples
pythonpandaslistdataframeunique-values

Merging unique values from two different columns into a list


How do I merge the unique values in columns A and B from df into list?

id  A   B
1   x   k
2   y   x
3   x   l
4   x   k

This is what I am trying to achieve:

list = ['x', 'y', 'k', 'l']

Failed attempt:

list = df['A'].unique().tolist()
list.append(df['B'].unique())

The output:

['x', 'y', ['x', 'k', 'l']]

Solution

  • Use np.ravel and np.unique:

    # Global unique
    >>> np.unique(np.ravel(df[['A', 'B']])).tolist()
    ['k', 'l', 'x', 'y']
    
    # Per column unique
    >>> df['A'].unique().tolist() + df['B'].unique().tolist()
    ['x', 'y', 'k', 'x', 'l']