Different Kinds of Joins for pandas DataFrames in Python (4 Examples)

In this tutorial you’ll learn how to apply different types of joins for pandas DataFrames in the Python programming language.

Setting up the Examples

import pandas as pd                                            # Import pandas library in Python
df1 = pd.DataFrame({"ID":range(1, 5),                          # Construct two pandas DataFrames
                    "A":range(11, 15),
                    "B":range(20, 16, - 1)})
print(df1)
#    ID   A   B
# 0   1  11  20
# 1   2  12  19
# 2   3  13  18
# 3   4  14  17
df2 = pd.DataFrame({"ID":range(3, 7),
                    "C":["a", "s", "d", "f"],
                    "D":range(10, 14)})
print(df2)
#    ID  C   D
# 0   3  a  10
# 1   4  s  11
# 2   5  d  12
# 3   6  f  13

Example 1: Inner Join Two pandas DataFrames in Python

df_merge_inner = pd.merge(df1, df2, on = "ID", how = "inner")  # Joining DataFrames
print(df_merge_inner)
#    ID   A   B  C   D
# 0   3  13  18  a  10
# 1   4  14  17  s  11

Example 2: Outer Join Two pandas DataFrames in Python

df_merge_outer = pd.merge(df1, df2, on = "ID", how = "outer")  # Joining DataFrames
print(df_merge_outer)
#    ID     A     B    C     D
# 0   1  11.0  20.0  NaN   NaN
# 1   2  12.0  19.0  NaN   NaN
# 2   3  13.0  18.0    a  10.0
# 3   4  14.0  17.0    s  11.0
# 4   5   NaN   NaN    d  12.0
# 5   6   NaN   NaN    f  13.0

Example 3: Left Join Two pandas DataFrames in Python

df_merge_left = pd.merge(df1, df2, on = "ID", how = "left")    # Joining DataFrames
print(df_merge_left)
#    ID   A   B    C     D
# 0   1  11  20  NaN   NaN
# 1   2  12  19  NaN   NaN
# 2   3  13  18    a  10.0
# 3   4  14  17    s  11.0

Example 4: Right Join Two pandas DataFrames in Python

df_merge_right = pd.merge(df1, df2, on = "ID", how = "right")  # Joining DataFrames
print(df_merge_right)
#    ID     A     B  C   D
# 0   3  13.0  18.0  a  10
# 1   4  14.0  17.0  s  11
# 2   5   NaN   NaN  d  12
# 3   6   NaN   NaN  f  13

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top