假设数据帧df具有三列c1, c2, c3

df=pd.DataFrame()
df['c1']=[1,2,3,3,4]
df['c2']=["a1","a2","a2","a2","a1"]
df['c3']=[1,2,3,3,5]
print df
df1=df[df.duplicated()]
print df1


df1只有一行,这是

    c1  c2  c3
3   3  a2   3


但我想要

    c1  c2  c3
2   3  a2   3
3   3  a2   3


如何获得?还有一件事,如果我尝试将参数'keep'用作df1 = df[df.duplicated(keep=False)],它会给我错误

 Traceback (most recent call last):

 File "<ipython-input-572-188a22102b3e>", line 1, in <module>
 df1 = df[df.duplicated(keep=False)]

 File "C:\Users\Kanika\Anaconda\lib\site-packages\pandas\util\decorators.py", line 88, in wrapper
  return func(*args, **kwargs)

TypeError: duplicated() got an unexpected keyword argument 'keep'

最佳答案

您为keep指定的值是多少?我认为,在您的情况下,将Keep值传递为False可能会解决问题。 Pandas Duplicated Doc's 。希望能帮助到你。

df1 = df[df.duplicated(keep=False)]

10-08 05:38
查看更多