根据以下数据集,我不希望获得唯一值的数量和唯一值的数量。
我的数据集:
Account_Type
Gold
Gold
Platinum
Gold
Run Code Online (Sandbox Code Playgroud)
输出 :
no of unique values : 2
unique values : [Gold,Platinum]
Gold : 3
Platinum :1
Run Code Online (Sandbox Code Playgroud)
用 pd.value_counts
pd.value_counts(df.Account_Type)
Gold 3
Platinum 1
Name: Account_Type, dtype: int64
Run Code Online (Sandbox Code Playgroud)
获取唯一数量
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
Run Code Online (Sandbox Code Playgroud)
替代方法
df['col1'].value_counts(sort=True)
df['col1'].value_counts(sort=True, normalize=True) -> provides proportion
Run Code Online (Sandbox Code Playgroud)