gpt4 book ai didi

python - 如何找出列中唯一值的数量以及数据框中唯一值的数量?

转载 作者:太空宇宙 更新时间:2023-11-04 00:38:01 25 4
gpt4 key购买 nike

根据以下数据集,我不想获取唯一值的数量和唯一值的计数。

我的数据集:

Account_Type
Gold
Gold
Platinum
Gold

输出:

no of unique values : 2
unique values : [Gold,Platinum]
Gold : 3
Platinum :1

最佳答案

使用pd.value_counts

pd.value_counts(df.Account_Type)

Gold 3
Platinum 1
Name: Account_Type, dtype: int64

同时获取唯一数量

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

替代方法

df['col1'].value_counts(sort=True)
df['col1'].value_counts(sort=True, normalize=True) -> provides proportion

关于python - 如何找出列中唯一值的数量以及数据框中唯一值的数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43066374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com