How to get all unique combinations of values ​​for specific columns

I want to extract all unique combinations of Col1 , Col2 and Col3 column values. Let's say the following df data file exists:

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

Answer:

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

I know how to get the unique values ​​of a specific column, i.e. df.Col1.unique() , however not sure of the unique combinations.

+5
source share
1 answer

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 :

 >>> 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 ​​for specific columns:

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

as @jezrael suggests, you can also use the subset drop_duplicates() parameter:

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

Source: https://habr.com/ru/post/1261830/


All Articles