Joining in Pandas

Written by

in

Python has Pandas its tool to handle matrices, also known as dataframes in Pyhon speak. It is possible to join dataframes, much like joining tables. I found a nice example on the internet with several possible choices.

As a first step, two dataframes were declared:

import pandas as pd

The first dataframe:

customer=pd.DataFrame({
'id':[1,2,3,4,5,6,7,8,9],
'name':['Olivia','Aditya','Cory','Isabell','Dominic','Tyler','Samuel','Daniel','Jeremy'],
'age':[20,25,15,10,30,65,35,18,23],
'Product_ID':[101,0,106,0,103,104,0,0,107],
'Purchased_Product':['Watch','NA','Oil','NA','Shoes','Smartphone','NA','NA','Laptop'],
'City':['Mumbai','Delhi','Bangalore','Chennai','Chennai','Delhi','Kolkata','Delhi','Mumbai']
})

The second dataframe:

product=pd.DataFrame({
'Product_ID':[101,102,103,104,105,106,107],
'Product_name':['Watch','Bag','Shoes','Smartphone','Books','Oil','Laptop'],
'Category':['Fashion','Fashion','Fashion','Electronics','Study','Grocery','Electronics'],
'Price':[299.0,1350.50,2999.0,14999.0,145.0,110.0,79999.0],
'Seller_City':['Delhi','Mumbai','Chennai','Kolkata','Delhi','Chennai','Bengalore']
})

The inner join:

ff_inner = pd.merge(product,customer,on= 'Product_ID')

An outer join

ff_outer = pd.merge(product,customer,left_on='Product_ID',right_on='Product_ID',how='outer',indicator=True)

The left join is as follows:

ff_left = pd.merge(product,customer,left_on=['Product_ID','Seller_City'],right_on=['Product_ID','City'],how='left')