Skip to content
Related Articles

Related Articles

How to Merge Two Pandas DataFrames on Index?

Improve Article
Save Article
Like Article
  • Last Updated : 13 Jan, 2022

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

Method 1: Using join() 

This method is used to join the dataframe based on index.

Syntax:

dataframe1.join(dataframe2)

Example:

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:

Method 2: Using merge()

This will merge the two dataframes with matching indexes 

Syntax:

pandas.merge(dataframe1, dataframe2, left_index=True, right_index=True)

where,

  • dataframe1 is the first dataframe
  • dataframe2 is the second dataframe
  • left_index specifies the first dataframe index set to be true
  • right_index specifies the second dataframe index set to be true

Example:

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 with merge
print(pd.merge(data1, data2, left_index=True, right_index=True))

Output:

Method 3: Using concat()

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

Syntax:

pandas.concat([daatframe1,dataframe2], axis=1)

Example:

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 with concat
print(pd.concat([data1, data2], axis=1))

Output:


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!