Search code examples
pythonpandasforeign-keysmerging-data

How do I merge two datasets based on the common key in pandas?


I have two datasets that contains domain names:

df1:

varA     domains            
123     www.google.com   
456     www.ebay.com     
789     www.amazon.com   
101     www.nbc.com      
....

df2:

 urls            varB
www.cnn.com      xsd
www.ebay.com     wer
www.nbc.com      xyz
www.amazon.com   zyx
....

I need to populate urls values in df2 with varA values from df1 for the matching domains/urls, so the output would look like this:

 urls            varA   varB
www.ebay.com     456    wer
www.nbc.com      101    xyz
www.amazon.com   789    zyx
....

All of the domains in df2 that do not have a matching domain in df1 should be removed.

I have this code:

target_cols = ['domains', 'urls', 'varB', 'varA']
df2.merge(df1[target_cols], on='urls', how='inner')

The code is generating an error.

How do I fix it? Any alternative solutions that can work?


Solution

  • The error is because keys on which you are merging do not have same name This will work

    pd.merge(df1, df2, left_on = 'domains', right_on = 'urls', how = 'inner').drop('domains', axis = 1)
    
    
        varA    urls            varB
    0   456     www.ebay.com    wer
    1   789     www.amazon.com  zyx
    2   101     www.nbc.com     xyz