gpt4 book ai didi

python-3.x - 如何检查系列中的值是否唯一

转载 作者:行者123 更新时间:2023-12-02 12:39:23 24 4
gpt4 key购买 nike

我正在尝试测试系列中的值是否是唯一值。我知道series.unique可以给出series中的一组唯一值,但不确定它如何适合我的情况。或者迭代series,但效率不是很高,所以我想知道是否有更好的方法来做到这一点?

最佳答案

您可以使用duplicated使用参数 keep=False,然后通过 ~ 反转 bool Series:

print df
col
0 a
1 b
2 a
3 b
4 c

print df.col.duplicated(keep=False)
0 True
1 True
2 True
3 True
4 False
Name: col, dtype: bool

print ~df.col.duplicated(keep=False)
0 False
1 False
2 False
3 False
4 True
Name: col, dtype: bool

关于python-3.x - 如何检查系列中的值是否唯一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36622023/

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