Merge & Join Two pandas DataFrames based on Column in Python (Example Code)

This tutorial explains how to merge two pandas DataFrames based on a particular variable in Python.

Preparing the Example

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

Example: Join Two pandas DataFrames based on Certain Variable in Python

df_merge = pd.merge(df1, df2, on = "my_col")    # Merging pandas DataFrames
print(df_merge)
#    my_col   A   B  C  D   E
# 0       3  12  16  a  1  11
# 1       4  13  14  s  2  12
# 2       5  14  12  a  3  13

Related Articles & Further Resources

Furthermore, you may want to read some of the related articles on my homepage. You can find a selection of related posts below.

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