Reputation: 1090
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
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
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
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