3

I am trying to return a df where duplicate values have been removed. I have tried to use drop.duplicates() but the values in the columns which have been subset aren't ordered. As in, the values are duplicates but they aren't in the same order.

For instance, using the df below, if I try to remove duplicate values from Item_X and Item_Y it will return the same df. Where the intended output will remove the second row.

import pandas as pd

d = ({
    'Item_X' : ['Foo','Bar','Bot','Bot','Bar','Foo'],                 
    'Item_Y' : ['Bar','Foo','Foo','Bot','Bar','Foo'],                 
    'Value' : [1,2,3,4,5,6],                      
     })

df = pd.DataFrame(data = d)

df.drop_duplicates(subset=['Item_X','Item_Y'])

Expected Result:

  Item_X Item_Y  Value
0    Foo    Bar      1
2    Bot    Foo      3
3    Bot    Bot      4
4    Bar    Bar      5
5    Foo    Foo      6

Actual Output (Incorrect):

  Item_X Item_Y  Value
0    Foo    Bar      1
1    Bar    Foo      2
2    Bot    Foo      3
3    Bot    Bot      4
4    Bar    Bar      5
5    Foo    Foo      6

What would be the most efficient way to tackle this problem?

2 Answers 2

4

You'll need to sort the columns along the horizontal axis, then get a mask to subset the original frame. Here's how you can use np.sort and df.duplicated to do that:

df[~pd.DataFrame(np.sort(df2[['Item_X', 'Item_Y']], axis=1)).duplicated()]

  Item_X Item_Y  Value
0    Foo    Bar      1
2    Bot    Foo      3
3    Bot    Bot      4
4    Bar    Bar      5
5    Foo    Foo      6
Sign up to request clarification or add additional context in comments.

2 Comments

We are on the same track here. :D +1
@anky_91 Yup, the answer is identical (!) except I specify axis=1 so it is obvious what it is doing.
3

IIUC, use:

m=pd.DataFrame(np.sort(df[['Item_X','Item_Y']])).duplicated()
df[~m]

  Item_X Item_Y  Value
0    Foo    Bar      1
2    Bot    Foo      3
3    Bot    Bot      4
4    Bar    Bar      5
5    Foo    Foo      6

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.