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

How to Merge Two Pandas DataFrames on Index

Last Updated : 22 Mar, 2023
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
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



Similar Reads

Joining two Pandas DataFrames using merge()
Let us see how to join two Pandas DataFrames using the merge() function. merge() Syntax : DataFrame.merge(parameters) Parameters : right : DataFrame or named Series how : {‘left’, ‘right’, ‘outer’, ‘inner’}, default ‘inner’ on : label or list left_on : label or list, or array-like right_on : label or list, or array-like left_index : bool, default F
2 min read
Pandas - Merge two dataframes with different columns
Pandas support three kinds of data structures. They are Series, Data Frame, and Panel. A Data frame is a two-dimensional data structure, Here data is stored in a tabular format which is in rows and columns. We can create a data frame in many ways. Here we are creating a data frame using a list data structure in python. C/C++ Code # import required
3 min read
Merge two Pandas dataframes by matched ID number
In this article, we will see how two data frames can be merged based on matched ID numbers. Approach Create a first data frameCreate a second data frameSelect Column to be matchedMerge using the merge function Syntax : DataFrame.merge(parameters) Display result Given below are implementations to produce a required result with the use of the require
4 min read
Merge two Pandas DataFrames with complex conditions
In this article, we let's discuss how to merge two Pandas Dataframe with some complex conditions. Dataframes in Pandas can be merged using pandas.merge() method. Syntax: pandas.merge(parameters) Returns : A DataFrame of the two merged objects. While working on datasets there may be a need to merge two data frames with some complex conditions, below
4 min read
Merge two Pandas DataFrames based on closest DateTime
In this article, we will discuss how to merge Pandas DataFrame based on the closest DateTime. To learn how to merge DataFrames first you have to learn that how to create a DataFrame for that you have to refer to the article Creating a Pandas DataFrame. After creating DataFrames need to merge them and to merge the Dataframe there's a function named
6 min read
Merge two Pandas DataFrames on certain columns
In data analysis, combining Pandas DataFrames is made easy with the merge function. You can streamline this process by pointing out which columns to use. Using a simple syntax, merging becomes a handy tool for efficiently working with data in various situations. This article walks you through the basic steps of merging Pandas DataFrames, providing
5 min read
How to Merge DataFrames of different length in Pandas ?
In this article, we will discuss how to merge the two dataframes with different lengths in Pandas. It can be done using the merge() method. Syntax: DataFrame.merge(parameters) Below are some examples that depict how to merge data frames of different lengths using the above method: Example 1: Below is a program to merge two student data frames of di
2 min read
How to Join Pandas DataFrames using Merge?
Joining and merging DataFrames is that the core process to start out with data analysis and machine learning tasks. It's one of the toolkits which each Data Analyst or Data Scientist should master because in most cases data comes from multiple sources and files. In this tutorial, you'll how to join data frames in pandas using the merge technique. M
3 min read
Python | Merge, Join and Concatenate DataFrames using Pandas
A dataframe is a two-dimensional data structure having multiple rows and columns. In a Pandas DataFrame, the data is aligned in the form of rows and columns only. A dataframe can perform arithmetic as well as conditional operations. It has a mutable size. This article will show how to join, concatenate, and merge in Pandas. Python Merge, Join, and
4 min read
Merge two DataFrames with different amounts of columns in PySpark
In this article, we will discuss how to perform union on two dataframes with different amounts of columns in PySpark in Python. Let's consider the first dataframe Here we are having 3 columns named id, name, and address. C/C++ Code # importing module import pyspark # import when and lit function from pyspark.sql.functions import when, lit # importi
6 min read
Practice Tags :
three90RightbarBannerImg