2

I have a Pandas DataFrame as follows;

data = pd.DataFrame({'A':[1,2,3,1,23,3,76,2,45,76],'B':[12,56,22,45,1,3,98,79,77,67]})

To remove duplicate values from the dataframe I have done this;

set(data['A'].unique()).union(set(data['B'].unique()))

which results in;

set([1, 2, 3, 12, 76, 77, 79, 67, 22, 23, 98, 45, 56])

Is there a better way of doing this? Is there a way of achieving this by using drop_duplicates?

Edit:

also, What if I had two more columns 'C' & 'D' but need to drop duplicates only from 'A' & 'B' ?

1 Answer 1

4

If you are intent on collapsing this

In [10]: np.unique(data.values.ravel())
Out[10]: array([ 1,  2,  3, 12, 22, 23, 45, 56, 67, 76, 77, 79, 98])

This will work as well

In [12]: data.unstack().drop_duplicates()
Out[12]: 
A  0     1
   1     2
   2     3
   4    23
   6    76
   8    45
B  0    12
   1    56
   2    22
   6    98
   7    79
   8    77
   9    67
dtype: int64
Sign up to request clarification or add additional context in comments.

2 Comments

cool! What if I had two more columns 'C' & 'D' but need to drop duplicates only from 'A' & 'B' ?
drop_duplicates takes a cols argument (so you can specify a list)

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.