6

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 
2

3 Answers 3

9

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 
Sign up to request clarification or add additional context in comments.

1 Comment

When applying this nice code to a categorical variable of a df I receive an error: pd.value_counts(df.application_type) s = pd.value_counts(df.application_type) s1 = pd.Series({'nunique': len(s), 'unique values': s.index.tolist()}) s.append(s1) TypeError: cannot append a non-category item to a CategoricalIndex "application_type" is a category column from df.
1

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

len(set(data_set))

To count the occurrence:

data_set.count(value)

Comments

0
 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()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.