Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在列组合中查找重复项_Python_Pandas_Duplicates - Fatal编程技术网

Python 在列组合中查找重复项

Python 在列组合中查找重复项,python,pandas,duplicates,Python,Pandas,Duplicates,我需要在两列中保留唯一的记录。 想象一下,在下面的数据帧(df)中,我想删除x列和y列中的重复信息 x y z 1 3 1 4 4 3 2 4 3 1 3 2 352 我所做的是连接XY=str(x)+str(y),并通过pd.unique(df.XY())保持唯一值。 记录(1 3 1)和(1 3 2)将是重复的 我相信一定有更好的方法来做到这一点。。。尤其是涉及到3列或更多列时。 谢谢 MB使用: 您可以使用参数keep保留first或last重复行: print df.drop_dupli

我需要在两列中保留唯一的记录。 想象一下,在下面的数据帧(df)中,我想删除x列和y列中的重复信息

x y z

1 3 1

4 4 3

2 4 3

1 3 2

352

我所做的是连接XY=str(x)+str(y),并通过pd.unique(df.XY())保持唯一值。 记录(1 3 1)和(1 3 2)将是重复的

我相信一定有更好的方法来做到这一点。。。尤其是涉及到3列或更多列时。 谢谢 MB使用:

您可以使用参数
keep
保留
first
last
重复行:

print df.drop_duplicates(subset=['x','y'])
#it is same as:
print df.drop_duplicates(subset=['x','y'], keep='first')
   x  y  z
0  1  3  1
1  4  4  3
2  2  4  3
4  3  5  2

print df.drop_duplicates(subset=['x','y'], keep='last')
   x  y  z
1  4  4  3
2  2  4  3
3  1  3  2
4  3  5  2
如果需要删除所有重复项,请使用
keep=False

print df.drop_duplicates(subset=['x','y'], keep=False)
   x  y  z
1  4  4  3
2  2  4  3
4  3  5  2

df.drop_duplicates(subset=['x','y'])
应该可以工作,您想保留第一个副本还是删除它?如果是后者,那么您需要将
keep=False
作为一个附加参数,这里有很多相关的问题:谢谢,我被单列副本淹没了。。。我在浏览问题时遇到了措辞上的问题。。。
print df.drop_duplicates(subset=['x','y'], keep=False)
   x  y  z
1  4  4  3
2  2  4  3
4  3  5  2