Using the pd.merge() method with a left join

The third option is to use the pd.merge() method with the left join technique. By now, you should have understood the concept of a merge. The argument of the pd.merge() method allows us to use different types of joins.

These are the following types of joins:

  • The inner join takes the intersection from two or more dataframes. It corresponds to the INNER JOIN in Structured Query Language (SQL).
  • The outer join takes the union from two or more dataframes. It corresponds to the FULL OUTER JOIN in SQL. 
  • The left join uses the keys from the left-hand dataframe only. It corresponds to the LEFT OUTER JOIN in SQL. 
  • The right join uses the keys from the right-hand dataframe only. It corresponds to the RIGHT OUTER JOIN in SQL. 

Let's see how we can use the left outer join:

dfSE = pd.concat([df1SE, df2SE], ignore_index=True)
dfML = pd.concat([df1ML, df2ML], ignore_index=True)

df = dfSE.merge(dfML, how='left')
df

The output of the preceding code is as follows:

If you look at the preceding screenshot, you can correctly answer how many students only appeared for the Software Engineering course. The total number would be 26. Note that these students did not appear for the Machine Learning exam and hence their scores are marked as NaN.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.9.169