#5, First Floor, 4th Street , Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 pro@slogix.in

Office Address

  • #5, First Floor, 4th Street Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 Landmark : Samiyar Madam
  • pro@slogix.in
  • +91- 81240 01111

Social List

How to merge or join data frames in python using pandas?
Description

To merge or join two or more data frame using pandas in python.

Input

Two Data frames.

Output

New data frame.

Process

   Import pandas library.

   Create two data frame.

  Merge the data frame using pd.merge().

   Merge the data frames using ‘on’ keyword.

   Merge the data frames using ‘how’ keyword.

Sapmle Code

#import libraries
import pandas as pd

#create first data frame
df1 = pd.DataFrame({‘Name’:[‘Sathish’,’Krishna’,’Praveen’],
‘Age’:[24,25,24],
‘salary’:[18000,19000,21000]})

#Create second data frame
df2 = pd.DataFrame({‘Name’:[‘Yusuf’,’Prakash’,’Ezhil’],
‘Age’:[24,26,27],
‘salary’:[32000,22000,19000]})

print(“First data frame \n”)
print(df1,”\n\n”,”Second data frame\n\n”,df2)
print(“\n”)

#Merging data frames using on key
print(“Merging data frames using ‘on’ key\n”)
print(pd.merge(df1,df2,on=’Age’))
print(“\n”)

#merging using how
print(“Merging data frames using ‘how(left)’ key\n”)
print(pd.merge(df1,df2,on=’Age’, how=’left’))
print(“\n”)
print(“Merging data frames using ‘how(right)’ key\n”)
print(pd.merge(df1,df2,on=’Age’, how=’right’))

Screenshots