The Wayback Machine - https://web.archive.org/web/20240204103240/https://www.geeksforgeeks.org/how-to-merge-two-pandas-dataframes-on-index/
Open In App
Related Articles

How to Merge Two Pandas DataFrames on Index

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In this article, we will discuss how to merge two Pandas Dataframes on Index in Python.

Merge two Pandas DataFrames on Index using join() 

This join() method is used to join the Dataframe based on the index.

Python3




# import pandas module
import pandas as pd
  
# create student  dataframe
data1 = pd.DataFrame({'id': [1, 2, 3, 4],
                      'name': ['manoj', 'manoja', 'manoji', 'manij']},
                     index=['one', 'two', 'three', 'four'])
  
  
# create marks  dataframe
data2 = pd.DataFrame({'s_id': [1, 2, 3, 6, 7],
                      'marks': [98, 90, 78, 86, 78]}, 
                     index=['one', 'two', 'three', 'siz', 'seven'])
  
# join two dataframes
print(data1.join(data2))


Output:

 

Merge Two Pandas DataFrames on Index using merge()

This merge() method will merge the two Dataframes with matching indexes 

Python3




# import pandas module
import pandas as pd
  
# join two dataframes with merge
print(pd.merge(data1, data2, left_index=True, right_index=True))


Output:

 

Merge Two Pandas DataFrames on Index using concat()

We can concatenate two Dataframes by using concat() method by setting axis=1.

Python3




# import pandas module
import pandas as pd
  
# join two dataframes with concat
print(pd.concat([data1, data2], axis=1))


Output:

 

Recommended Article: Pandas Merging, Joining, and Concatenating


Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.

Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.


Last Updated : 22 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials