2
$\begingroup$

I have the following data:

userid itemid
  1       1
  1       1
  1       3
  1       4
  2       1
  2       2
  2       3

How can I drop duplicated items (not the index) to make the following DataFrame:

userid itemid
  1       1
  1       3
  1       4     
  2       1
  2       2
  2       3

Can someone help me?

import pandas as pd    
df = pd.DataFrame({'userid':[1,1,1,1, 2,2,2],
                   'itemid':[1,1,3,4, 1,2,3] })
$\endgroup$
  • 4
    $\begingroup$ df.drop_duplicates() ? $\endgroup$ – Andrey Lukyanenko Aug 2 '17 at 12:44
  • 3
    $\begingroup$ Please accept an answer if the problem is solved, otherwise people will think it isn't and waste their time contributing solutions. $\endgroup$ – Emre Aug 7 '17 at 21:46
9
$\begingroup$

The best way would be to use drop_duplicates(). If you have a larger DataFrame and only want those two columns checked, set subset equal to the combined columns you want checked.

df = df.drop_duplicates()

or

df = df.drop_duplicates(subset=['userid', 'itemid'])
$\endgroup$
2
$\begingroup$

To avoid reassignment, use (inplace = True)

df.drop_duplicates(inplace=True)

This is same as

df = df.drop_duplicates()
$\endgroup$
1
$\begingroup$
df.groupby(df.index).first() 

worked for me.

$\endgroup$
1
$\begingroup$

Use drop_duplicates() of pandas:

import pandas as pd    
df = pd.DataFrame({'userid':[1,1,1,1, 2,2,2],
                   'itemid':[1,1,3,4, 1,2,3] })
print(df)
print()
print(df.drop_duplicates())

Consider that drop won't change the df itself and just pass a new data frame which has dropped the specified row(s). If you want to change the df itself set inside parameter to True.

$\endgroup$
-1
$\begingroup$

Using the following code:

df = df.drop_duplicates(['userid','itemid'])
$\endgroup$
  • $\begingroup$ This question is over one year old and already has several solutions, some of which are practically identical to yours... $\endgroup$ – Mark.F Dec 30 '18 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.