Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get All Unique Values in a Row

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).

like image 638
ScientiaEtVeritas Avatar asked Nov 22 '17 16:11

ScientiaEtVeritas


People also ask

How do you find unique values in Excel across multiple rows?

Select Text option from the Formula Type drop down list; Then choose Extract cells with unique values (include the first duplicate) from the Choose a fromula list box; In the right Arguments input section, select a list of cells that you want to extract unique values.

How do I get unique values in a row?

In Excel, there are several ways to filter for unique values—or remove duplicate values: To filter for unique values, click Data > Sort & Filter > Advanced. To remove duplicate values, click Data > Data Tools > Remove Duplicates.

How do I get unique values from a row in pandas?

You can get unique values in column (multiple columns) from pandas DataFrame using unique() or Series. unique() functions. unique() from Series is used to get unique values from a single column and the other one is used to get from multiple columns.

How do I get a list of unique values from a table?

Excel has a very useful feature called remove duplicates which can be used on either the whole data set or a single column of data. Copy and paste the column of values which you want to see a list of unique values from to another location. Highlight this list of values.


3 Answers

list(map(set,df.values))
Out[72]: [{1, 2}, {2, 3}, {4, 5, 9}]
like image 166
BENY Avatar answered Oct 27 '22 07:10

BENY


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
like image 34
MaxU - stop WAR against UA Avatar answered Oct 27 '22 06:10

MaxU - stop WAR against UA


Lets use pd.unique i.e

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

        0       1          2
unique  [1, 2]  [2, 3]  [5, 4, 9]
like image 41
Bharath Avatar answered Oct 27 '22 05:10

Bharath