Merging unique values from two different columns into a list

96 Views Asked by At

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']]
2

There are 2 best solutions below

0
Corralien On BEST ANSWER

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']
2
jezrael On

Use pandas.unique with numpy.ravel for unique values in original ordering:

L = pd.unique(np.ravel(df[['A','B']], order='F')).tolist()

Another solutions with DataFrame.unstack or DataFrame.melt and Series.unique

L = df[['A','B']].unstack().unique().tolist()
L = df[['A','B']].melt()['value'].drop_duplicates().tolist()
L = df[['A','B']].melt()['value'].unique().tolist()
L = df[['A','B']].melt()['value'].drop_duplicates().tolist()
print (L)
['x', 'y', 'k', 'l']