Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas – drop row if another row has the same values but the columns are switched

as you can see below some rows are the same except the value in column NUM1 and value in column NUM2 are switched. I need only one of them.

Dropping rows based on last three columns is not the right answer, because the table can contain other records with same dates and used column value, but with different NUM1 and NUM2.

    NUM1    NUM2    DATE1         DATE2          USED
0   4       7       10. 9. 2002   16. 11. 2002   5,3
1   7       4       10. 9. 2002   16. 11. 2002   5,3
2   9       10      10. 9. 2002   16. 11. 2002   3,1
3   10      9       10. 9. 2002   16. 11. 2002   3,1

New table should look like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    NUM1    NUM2    DATE1         DATE2          USED
0   4       7       10. 9. 2002   16. 11. 2002   5,3
1   9       10      10. 9. 2002   16. 11. 2002   3,1

Thanks for help!

>Solution :

Try with frozenset

out = df[~df.apply(frozenset,1).duplicated()]
Out[906]: 
   NUM1  NUM2      DATE1       DATE2 USED
0     4     7  10.9.2002  16.11.2002  5,3
2     9    10  10.9.2002  16.11.2002  3,1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading