daiyue
daiyue

Reputation: 7448

How to check a value in a series is unique

I am trying to test out if a value in a series is a unique value. I know series.unique can give an array of unique values in the series, but not sure how it fit in my case. Or iterate through the series, but it's not very efficient, so I am wondering is there a better way to do that?

Upvotes: 2

Views: 159

Answers (2)

Thanos
Thanos

Reputation: 2572

You could combine boolean indexing and built-in len() to get the exact amount of instances of a given value.

Assuming your DataFrame looks like this:

In [155]: test_1 = pd.DataFrame(np.array([1,2,3,3,4]), columns=['A'])

In [156]: test_1
Out[156]: 
   A
0  1
1  2
2  3
3  3
4  4

You can get the amount of instances of value 3 or 1 like this:

In [158]: len(test_1[test_1.A == 3])
Out[158]: 2 ## two instances make for non-unique value

In [159]: len(test_1[test_1.A == 1])
Out[159]: 1 ## Unique value!!

This way you can define the exact value you want to test and get the amount of instances of this value.

Upvotes: 1

jezrael
jezrael

Reputation: 862511

You can use duplicated with parameter keep=False and then invert boolean Series by ~:

print df
  col
0   a
1   b
2   a
3   b
4   c

print df.col.duplicated(keep=False)
0     True
1     True
2     True
3     True
4    False
Name: col, dtype: bool

print ~df.col.duplicated(keep=False)
0    False
1    False
2    False
3    False
4     True
Name: col, dtype: bool

Upvotes: 6

Related Questions