Sidhartha
Sidhartha

Reputation: 1090

How to find out number of unique values in a column along with count of the unique values in a dataframe?

As per the following data set I want no get the number of unique values and count of the unique values.

My data set:

Account_Type
Gold
Gold
Platinum
Gold

Output :

no of unique values : 2
unique values : [Gold,Platinum]
Gold : 3
Platinum :1 

Upvotes: 6

Views: 45991

Answers (3)

piRSquared
piRSquared

Reputation: 294218

Use pd.value_counts

pd.value_counts(df.Account_Type)

Gold        3
Platinum    1
Name: Account_Type, dtype: int64

Get number of unique as well

s = pd.value_counts(df.Account_Type)
s1 = pd.Series({'nunique': len(s), 'unique values': s.index.tolist()})
s.append(s1)

Gold                            3
Platinum                        1
nunique                         2
unique values    [Gold, Platinum]
dtype: object

Alternate Approach

df['col1'].value_counts(sort=True)
df['col1'].value_counts(sort=True, normalize=True) -> provides proportion

Upvotes: 9

missnomer
missnomer

Reputation: 145

    df['Account_Type].unique() 

returns unique values of the specified column (in this case 'Account_Type') as a NumPy array.

All you have to do is use the len() function to find the no of unique values in the array.

    len(df['Account_Type].unique()) 

To find the respective counts of unique values, you can use value_counts()

Upvotes: 0

adabsurdum
adabsurdum

Reputation: 91

You can use set() to remove duplicates and then calculate the length:

len(set(data_set))

To count the occurrence:

data_set.count(value)

Upvotes: 1

Related Questions