Merge & Join pandas DataFrames based on Row Index in Python (Example Code)

In this tutorial you’ll learn how to join two DataFrames based on index values in the Python programming language.

Example Data

import pandas as pd                       # Load pandas
df1 = pd.DataFrame({"A":range(10, 13),    # Construct two pandas DataFrames
                    "B":range(13, 10, - 1)},
                     index = list("abc"))
print(df1)
#     A   B
# a  10  13
# b  11  12
# c  12  11
df2 = pd.DataFrame({"C":["a", "d", "s", "d", "s", "s"],
                    "D":range(1, 7)},
                     index = list("bcdefg"))
print(df2)
#    C  D
# b  a  1
# c  d  2
# d  s  3
# e  d  4
# f  s  5
# g  s  6

Example: Joining pandas DataFrames based on Index Values

df_join = pd.merge(df1,                   # Outer join using row index
                   df2,
                   left_index = True,
                   right_index = True,
                   how = "outer")
print(df_join)
#       A     B    C    D
# a  10.0  13.0  NaN  NaN
# b  11.0  12.0    a  1.0
# c  12.0  11.0    d  2.0
# d   NaN   NaN    s  3.0
# e   NaN   NaN    d  4.0
# f   NaN   NaN    s  5.0
# g   NaN   NaN    s  6.0

Related Tutorials

Have a look at the following Python tutorials. They illustrate topics such as data inspection, naming data, and indices:

Leave a Reply

Your email address will not be published.

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