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:
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).

