Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pandas

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 
like image 940
Sidhartha Avatar asked Mar 28 '17 09:03

Sidhartha


2 Answers

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
like image 135
piRSquared Avatar answered Oct 01 '22 01:10

piRSquared


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

len(set(data_set))

To count the occurrence:

data_set.count(value)

like image 35
adabsurdum Avatar answered Sep 30 '22 23:09

adabsurdum