In this python pandas program, we will Join the two Dataframes using the common column and value using the pandas library.
Steps to solve the program
- Import pandas library as pd.
- Create two dataframes using pd.DataFrame().
- We will perform inner join to join the two Dataframes using the common column and value using pd.merge(df1, df2, on=’Id’, how=’inner’).
- We will perform an inner join on the id column.
- Print the output.
import pandas as pd
df1 = pd.DataFrame({'Id':['S1','S2','S3'],
'Name':['Ketan','Yash','Abhishek'],
'Marks':[90,87,77]})
df2 = pd.DataFrame({'Id':['S2','S4'],
'Name':['Yash','Gaurav'],
'Marks':[70,65]})
print('Dataframe 1: \n',df1)
print('Dataframe 2: \n',df2)
new = pd.merge(df1, df2, on='Id', how='inner')
print("Merged data:")
print(new)
Output :
Dataframe 1:
Id Name Marks
0 S1 Ketan 90
1 S2 Yash 87
2 S3 Abhishek 77
Dataframe 2:
Id Name Marks
0 S2 Yash 70
1 S4 Gaurav 65
Merged data:
Id Name_x Marks_x Name_y Marks_y
0 S2 Yash 87 Yash 70