Dinosaurius
Dinosaurius

Reputation: 8628

How to obtain all unique combinations of values of particular columns

I want to extract all unique combinations of values of columns Col1, Col2 and Col3. Let's say there is the following dataframe df:

df =

Col1    Col2    Col3
12      AB      13
11      AB      13
12      AB      13
12      AC      14

The answer is:

unique =

Col1    Col2    Col3
12      AB      13
11      AB      13
12      AC      14

I know how to obtain unique values of a particular column, i.e. df.Col1.unique(), however not sure about unique combinations.

Upvotes: 21

Views: 20575

Answers (2)

Paul Rougieux
Paul Rougieux

Reputation: 11399

You could also use a unique() method on the index:

index = ['Col2','Col3']
df.set_index(index).index.unique().to_frame(False)

And you might be interested in knowing the number of time each combination is repeated using value_count as explained in this answer

df[index].value_counts()

Upvotes: 1

roman
roman

Reputation: 117380

There is a method for this - pandas.DataFrame.drop_duplicates:

>>> df.drop_duplicates()
   Col1 Col2  Col3
0    12   AB    13
1    11   AB    13
3    12   AC    14

You can do it inplace as well:

>>> df.drop_duplicates(inplace=True)
>>> df
   Col1 Col2  Col3
0    12   AB    13
1    11   AB    13
3    12   AC    14

If you need to get unique values of certain columns:

>>> df[['Col2','Col3']].drop_duplicates()
  Col2  Col3
0   AB    13
3   AC    14

as @jezrael suggests, you can also consider using subset parameter of drop_duplicates():

>>> df.drop_duplicates(subset=['Col2','Col3'])
   Col1 Col2  Col3
0    12   AB    13
3    12   AC    14

Upvotes: 28

Related Questions