Skip to main content
 首页 » 编程设计

python之如何将我放入 Python Pandas 数据框中的重复项写入文件

2024年12月31日8myhome

输入:

Col1    Col2 
1       apple 
2       apple 
3       orange 
4       grape 
5       grape 
6       grape 

这是我正在运行的删除重复项的程序。

#Drop Duplicates if any keep first 
sortfile1.drop_duplicates(['Col2'],keep = 'first', inplace = True) 

现在我的数据框看起来像这样......这是正确的。

输出:

 Col1    Col2 
1       apple 
3       orange 
4       grape 

这是问题所在。我需要显示从数据框中删除的记录。

所以我还需要一个输出文件来显示我删除的记录。

需要输出文件:(我需要一个包含我删除的记录的 csv 文件,就像这样)

Col1    Col2 
2       apple 
5       grape 
6       grape 

请您参考如下方法:

那么你应该使用duplicated

df[df.Col2.duplicated()] 
Out[396]:  
   Col1   Col2 
1     2  apple 
4     5  grape 
5     6  grape