python: separate out rows which have duplicates in panda dataframe

前提是你 提交于 2020-01-04 05:32:06

问题


Suppose a data frame df has three columns 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 has only one row, which is

    c1  c2  c3
3   3  a2   3

but I want to have

    c1  c2  c3
2   3  a2   3
3   3  a2   3

How to get it? One more thing, if I try to use argument 'keep' as df1 = df[df.duplicated(keep=False)], it gives me error

 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'

回答1:


What is the value that you specified for keep . I think, In your case Passing False as the keep value might solve the issue. Pandas Duplicated Doc's . Hope it helps.

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



回答2:


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

this option delete all duplicates, defalult pandas keep first appear.



来源:https://stackoverflow.com/questions/37154268/python-separate-out-rows-which-have-duplicates-in-panda-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!