Get All Unique Values in a Row

Viewed 10864

I have a DataFrame df1 that looks like this:

A       B       C
-----------------
1       1       2
2       2       3
5       4       9

I want to get all the unique values in a row. For example 1 and 2 in the first row. 2, 3 in the second row. And 4, 5 and 9 in the third row.

Result can vary, I can imagine a new column that contains a list with unique values or replacing duplicates with None would be also okay (or something else, maybe there is something more pythonic for this case).

3 Answers
list(map(set,df.values))
Out[72]: [{1, 2}, {2, 3}, {4, 5, 9}]
In [88]: df.stack().groupby(level=0).apply(lambda x: x.unique().tolist())
Out[88]:
0       [1, 2]
1       [2, 3]
2    [5, 4, 9]
dtype: object

Lets use pd.unique i.e

df.T.agg([pd.unique])

        0       1          2
unique  [1, 2]  [2, 3]  [5, 4, 9]
Related