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
You can use set()
to remove duplicates and then calculate the length:
len(set(data_set))
To count the occurrence:
data_set.count(value)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With