aerin
aerin

Reputation: 22634

How to check every pandas series value is unique

I know how to count the number of unique values in pandas series (one column in pandas dataframe).

pandas.Series.value_counts

But how do I check if they are all unique? Should I just compare value_counts with its length?

Upvotes: 30

Views: 47837

Answers (4)

Christopher Breen
Christopher Breen

Reputation: 21

pd.Series([1, 2, 3, np.nan, np.nan]).dropna().is_unique
True

Will solve the problem posed by swapnil athawale

Upvotes: 2

swapnil athawale
swapnil athawale

Reputation: 31

pd.Series([1,2,3,np.nan,np.nan]).is_unique

> False

It will give False for this which it should not.

Upvotes: 3

piRSquared
piRSquared

Reputation: 294218

IIUC, pd.Series.is_unique

pd.Series([1, 2, 3]).is_unique
True

And

pd.Series([1, 2, 2]).is_unique
False

Upvotes: 71

BENY
BENY

Reputation: 323226

You can use nunique

pd.Series([1, 2, 3]).nunique()==len(pd.Series([1, 2, 3]))
Out[62]: True

Upvotes: 4

Related Questions