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