Kanika Singhal
Kanika Singhal

Reputation: 655

python: separate out rows which have duplicates in panda dataframe

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'

Upvotes: 2

Views: 938

Answers (2)

mandrewcito
mandrewcito

Reputation: 170

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

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

Upvotes: 0

Strik3r
Strik3r

Reputation: 1057

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)]

Upvotes: 2

Related Questions