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

Python Pandas – Compare rows in same dataset on different colomns

I have this dataset:

DF1

| ID    | artshort  | artlong  |
| 248   | 123       |          |
| 249   | 456-1     |          |
| 100   | 456       | 456-1    |

As a result I want a dataset with the rows where artshort of one row is the same as artlong of another row.

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

I tried:

dftest = df1['artshort'].isin(df1['artlong'])]

But this is not working. Not sure if "isin" is the best approach in this case

>Solution :

You can try filtering using this condition:

data = {'ID': [248, 249, 100],
        'artshort': ['123', '456-1', '456'],
        'artlong': ['', '', '456-1']}

df1 = pd.DataFrame(data)

result = df1[df1['artshort'].isin(df1['artlong'])]

Output:

    ID  artshort    artlong
1   249 456-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