Friday, November 27, 2020

Pandas Inner and outer joins

If there are two Dataframes, then inner merge will result in rows that are only matching and outer merge will be giving all the ones that are 

Also non matching. If thee columns are different in these two dfs, then the missing columns on the lesser column df will be substituted with 

NULL 

data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'], 

        'Age':[27, 24, 22, 32], 

        'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'], 

        'Qualification':['Msc', 'MA', 'MCA', 'Phd']} 

  

# Convert the dictionary into DataFrame  

df = pd.DataFrame(data) 


data2 = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj', 'Retheesh'], 

        'Score':[2, 24, 22, 32, 50], 

        } 

  

# Convert the dictionary into DataFrame  

df2 = pd.DataFrame(data2) 

  

# select two columns from second data frame

df2 = df2[['Name', 'Score']] 


df_merged = df2.merge(df, how = 'outer', on='Name')

print('merged df')

print(df_merged)



Below is response of outer merge output 


merged df

       Name  Score   Age    Address Qualification

0       Jai      2  27.0      Delhi           Msc

1    Princi     24  24.0     Kanpur            MA

2    Gaurav     22  22.0  Allahabad           MCA

3      Anuj     32  32.0    Kannauj           Phd

4  Retheesh     50   NaN        NaN           NaN



References:

https://stackoverflow.com/questions/45175060/merge-dataframes-with-matching-values-from-two-different-columns-pandas

No comments:

Post a Comment