简体   繁体   English

在 Pandas 中除 0 之外的列中查找重复项

[英]Finding Duplicates In a column Except 0 in Pandas

I have a Dataframe with Position varying starting from 0. So I have to check whether the Position is having duplicate values except 0.As 0 can be present multiple times in my case.我有一个位置从 0 开始变化的数据框。所以我必须检查位置是否具有除 0 以外的重复值。在我的情况下,0 可以多次出现。

if df['Position'].duplicated().any():
 print("Duplicate Positions found..Positions should be unique..Exiting")

This will check for unique values including 0.Is there any way I can exclude 0 while finding duplicates?这将检查包括 0 在内的唯一值。有什么办法可以在查找重复项时排除 0?

You can chain mask by test for not 0 values by Series.ne :您可以通过Series.ne测试非0值来Series.ne

df = pd.DataFrame({'Position':[0,1,2,0]})
    
if (df['Position'].duplicated() & df['Position'].ne(0)).any():
    print("Duplicate Positions found..Positions should be unique..Exiting")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM