Yared J.
Yared J.

Reputation: 241

List only unique values in column

I want to filter out repeated values and list only unique values in my DataFrame.

Lets say I have a df like this:

     A  B
  0  1  3
  1  2  4
  2  3  3
  3  1  1
  4  3  0

And I want to list only the unique values in column 'A'

My desired df

 ['2']

I tried .drop_duplicates and .unique, but no luck.

Upvotes: 0

Views: 183

Answers (1)

user2285236
user2285236

Reputation:

df[~df['A'].duplicated(keep=False)]
Out[100]: 
   A  B
1  2  4

Or, if you want only the column A:

df.loc[~df['A'].duplicated(keep=False), 'A']
Out[102]: 
1    2
Name: A, dtype: int64

drop_duplicates would also work like this:

df.drop_duplicates(subset=['A'], keep=False)['A']
Out[105]: 
1    2
Name: A, dtype: int64

More compact:

df['A'].drop_duplicates(keep=False)
Out[106]: 
1    2
Name: A, dtype: int64

Upvotes: 4

Related Questions