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: join dataframes and filter rows by condition

I would appreciate some help on this: I’ve merged two tables with this structure and I’d need to get the list of items with different LOCNAME in both tables

Input:

enter image description here

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

Expected list output:
enter image description here

My code:

import pandas as pd

Madrid1 = pd.read_csv('Madrid1.csv')
Madrid2 = pd.read_csv('Madrid2.csv')

merged = pd.merge(Madrid1, Madrid2, how='inner', left_on='LocID', right_on='LocID').head(1)
filter = Madrid1['LocName_'] != (Madrid2['LocName_']) # THIS IS NOT CORRECT!

>Solution :

To get the list of items with different LOCNAME in both tables, you can use the loc function in Pandas with a condition that checks for the inequality between the LocName_ column in Madrid1 and Madrid2. Here’s the modified code:

import pandas as pd

Madrid1 = pd.read_csv('Madrid1.csv')
Madrid2 = pd.read_csv('Madrid2.csv')

merged = pd.merge(Madrid1, Madrid2, how='inner', left_on='LocID', right_on='LocID')
filtered = merged.loc[merged['LocName__x'] != merged['LocName__y'], 'LocID':'LocName__x']

The merged DataFrame contains all the rows that have matching LocID in both tables. The loc function then selects only the rows where the LocName__x column (from Madrid1) is not equal to the LocName__y column (from Madrid2). Finally, the result contains only the LocID and LocName__x columns (from Madrid1).

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