Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Join in Python
3.
Join in Pandas
3.1.
Inner Join 
3.2.
Outer Join
3.3.
Left Outer Join 
3.4.
Right Outer Join
4.
Frequently Asked Questions 
4.1.
What is Join in Python?
4.2.
What do you mean by a Dataframe?
4.3.
How many joins can we perform in Pandas?
4.4.
State the difference between Left Outer Join and Right Outer Join.
4.5.
What is returned if there is no match found in the table?
5.
Conclusion
Last Updated: Jun 30, 2023
Easy

Join In Python

Author Geetika Dua
0 upvote

Introduction

We know how engaging the language of python is! It is a versatile language that does wonders. Do you know about Join in python? If not, Don't worry. We got you covered!

introduction

Keep reading to learn about Join in Python.

Join in Python

The first question that may arise in your mind is, what exactly is a join in python?

Join in Python is an inbuilt string function. It is used to join sequence elements separated by a string separator. The job of the merge function is to join the elements of a sequence and makes it a string. 

Syntax

string_name.join()

sample = ('we', 'are’, 'coding', 'ninjas')
# put any character to join
s = "."
s = s.join(sample)
print(s)
You can also try this code with Online Python Compiler
Run Code
join in python

Join in Pandas

In Pandas, we can join or merge two data frames by using the merge() function. We can perform natural, left, right, and outer join in Pandas. We’ll further study these in this blog. Let us look at the syntax of how to join two data frames.

Syntax-

merge(df1,df2, on =’ID’, how=’type_of_join’)
You can also try this code with Online Python Compiler
Run Code

 

First of all, Let us create Dataframe

import pandas as pd
import numpy as np
d1 = {'Student_id':pd.Series([1,2,3,4,5,6]),
  'Names':pd.Series(['Aman ',' Raghav' ,'Rahul','Shubh','Usha','Surbhi'])}
df1 = pd.DataFrame(d1)
d2 = {'Student_id':pd.Series([2,4,6,7,8]),
    'Residence':pd.Series(['New York','New York','Texas','Singapore','Indiana'])}
df2 = pd.DataFrame(d2)
You can also try this code with Online Python Compiler
Run Code
Output of dataframe

Inner Join 

Inner Join returns only those rows in which the left table has matching keys in the right table.

inner join

The syntax of the same is-

pd.merge(df1, df2, on='Student_id', how='inner')
You can also try this code with Online Python Compiler
Run Code

 

After we run this code, the following output appears-

output

Outer Join

Outer Join in Pandas returns all the rows from both tables. It displays join records on the left side with matching keys in the right table. If no match exists from any table NaN will be returned.

outer join

The syntax of the same is-

pd.merge(df1, df2, on='Student_id', how='outer').
You can also try this code with Online Python Compiler
Run Code

 

After we run this code, the following output appears-

output

Left Outer Join 

Left Outer Join in Pandas returns all rows from the left table and the rows with matching keys from the right table. When there is no Matching from the right table, NaN will be returned.

left outer join

The syntax of the same is-

pd. merge(df1, df2, on='Student_id', how='left')
You can also try this code with Online Python Compiler
Run Code

 

The resultant data frame will be

output

Right Outer Join

Right Outer Join returns all rows from the right table. It also returns any rows with matching keys from the left table.

right outer join

The syntax of the same is-

pd. merge(df1, df2, on='Student_id', how='right') 
You can also try this code with Online Python Compiler
Run Code

 

The resultant data frame will be

output

Frequently Asked Questions 

What is Join in Python?

Join in Python is an inbuilt string function. It is used to join sequence elements separated by a string separator. The job of the merge function is to join the elements of a sequence and makes it a string. 

What do you mean by a Dataframe?

A Dataframe is a data structure that categorizes data into a 2-dimensional table of rows and columns. Dataframes are one of the most common data structures used in modern data analytics.

How many joins can we perform in Pandas?

We can perform natural, left, right, and outer join in Pandas.

State the difference between Left Outer Join and Right Outer Join.

Left Outer Join in Pandas returns all rows from the left table and the rows with matching keys from the right table. Right, Outer Join returns all rows from the right table. It also returns any rows with matching keys from the left table.

What is returned if there is no match found in the table?

If no match exists from any table, NaN will be returned.

Conclusion

This blog covered the Join in python. It introduced the concept of join in Pandas and explained the different types of Join in Python. We hope you are clear with this topic of Join in Python. Eager to learn more? Refer to our other blogs-

 

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Happy Coding!

 

Live masterclass