how to merge two df based on index in python

To merge two dataframes based on the index in Python using the pandas library, you can use the merge function with the left_index and right_index parameters set to True.

Here's an example:

main.py
import pandas as pd

# Create two sample dataframes
df1 = pd.DataFrame({'A': [1, 2, 3],
                    'B': [4, 5, 6]},
                   index=['X', 'Y', 'Z'])

df2 = pd.DataFrame({'C': [7, 8, 9],
                    'D': [10, 11, 12]},
                   index=['X', 'Y', 'Z'])

# Merge the dataframes based on index
merged_df = df1.merge(df2, left_index=True, right_index=True)

print(merged_df)
405 chars
16 lines

Output:

main.py
   A  B  C   D
X  1  4  7  10
Y  2  5  8  11
Z  3  6  9  12
60 chars
5 lines

In the above example, df1 and df2 are merged based on their index values. The resulting dataframe, merged_df, contains all the columns from both dataframes, with the index serving as the common joining key.

Note that if your dataframes have different indexes, the merge operation may result in missing values or duplicate rows. You can handle these cases using the how parameter to specify the type of merge operation you want.

related categories

gistlibby LogSnag