[英]Pandas/Python, if statement for df.duplicated
have a simple code that finds duplicate rows and prints them out if any.有一个简单的代码可以找到重复的行并将它们打印出来(如果有的话)。 It works fine without the IF statement, so it always prints:
没有 IF 语句它工作正常,所以它总是打印:
duplicated rows in the sheet :
Empty DataFrame
Columns: [IP,MAC,DNS,TEST,TEST2]
Index: []
I would like to add an IF statement to print out results only if the duplicated values have been found.我想添加一个 IF 语句以仅在找到重复值时打印结果。 Otherwise print "no duplicates"
否则打印“无重复”
import pandas as pd
df = pd.DataFrame(values,columns=['IP','MAC','DNS','TEST','TEST2'])
print('\n you are working on this data: \n',df)
if df.duplicated()==True:
duplicatedRows = df[df.duplicated()]
print('\n duplicated rows in the sheet : \n',duplicatedRows)
x= input('\n Please type "y" to continue or "x" exit\n').lower()
else:
print ('no duplicates')
DataFrame has duplicated method which you can use to get duplicate rows like below DataFrame 具有重复的方法,您可以使用它来获取重复的行,如下所示
df = pd.DataFrame(np.random.choice(3, (10,3)), columns = "A B C".split())
duplicated = df[df.duplicated()]
if len(duplicated):
print(duplicated)
else:
print("no duplicates")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.