
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Concatenate More Than Two Pandas DataFrames
In this article, we will explore how to concatenate more than two pandas DataFrames using the pandas and numpy modules. Typically, the pandas.concat() method is used to concatenate multiple data frames. This method allows for concatenation along rows (axis=0) or columns (axis=1), providing flexibility in combining data efficiently.
A DataFrame in Python's pandas library is a two-dimensional labeled data structure that is used for data manipulation and analysis. It can handle different data types such as integers, floats, and strings. Each column has a unique label, and each row is labeled with a unique index value, which helps in accessing specific rows.
Various ways to Concatenate two data frames
Following are different ways to concatenate more than two pandas data frames ?
Creating data frames
Before concatenating data frames, let us create two data frames. In the following example we have created two data frames using numpy module and pandas ?
import pandas import numpy data_frame1 = pandas.DataFrame({'Student': ["Ram", "Neha", "John", "Sakshi"], 'Marks': [95, 80, 90, 89]}) data_frame2 = pandas.DataFrame({'Student': ["Akash", "Bhanu", "krishna", "Rani"], 'Marks': [79, 85, 96, 80]}) print("DataFrame 1:") print(data_frame1) print() print("DataFrame 2:") print(data_frame2)
Output
Following is the output of the above code ?
DataFrame 1: Student Marks 0 Ram 95 1 Neha 80 2 John 90 3 Sakshi 89 DataFrame 2: Student Marks 0 Akash 79 1 Bhanu 85 2 krishna 96 3 Rani 80
Using concat() Method
The concat() method in pandas is used to combine two or more DataFrames either vertically (along rows) or horizontally (along columns). When axis=0, the DataFrames are concatenated vertically by stacking rows. When axis=1, they are concatenated horizontally by aligning columns. The sort=False parameter can be used to enhance performance by skipping the sorting of non-concatenation axis labels in the resulting data frame.
Example
Following is an example of concatenating data frames using the concat() method ?
import pandas import numpy data_frame1 = pandas.DataFrame({'Students': ["Ram", "Neha", "John", "Sakshi"], 'Marks': [95, 80, 90, 89]}) data_frame2 = pandas.DataFrame({'Students': ["Akash", "Bhanu", "krishna", "Rani"], 'Marks': [79, 85, 96, 80]}) concat_dataframe = pandas.concat([data_frame1, data_frame2], ignore_index=True, sort=False) print(concat_dataframe)
Following is the output of the above code ?
Students Marks 0 Ram 95 1 Neha 80 2 John 90 3 Sakshi 89 4 Akash 79 5 Bhanu 85 6 krishna 96 7 Rani 80
Using join() Method
The join() method in pandas is used to combine DataFrames based on their indices or specified columns. It aligns rows or columns from two DataFrames using common index values or column keys, supporting various types of joins such as inner, outer, left, or right. This method provides a flexible way to merge data while preserving the specified join logic.
Example
In the following example, we have combined two data frames using the join() method ?
import pandas import numpy data_frame1 = pandas.DataFrame({'Class1': ["Ram", "Neha", "John","Sam"],'Maths_Marks': [95, 80, 90, 89]}) data_frame2 = pandas.DataFrame({'Class2': ["Akash", "Bhanu","krishna", "Rani"], 'Physics_Marks': [79, 85, 96, 80]}) concat_dataframe = data_frame2.join(data_frame1) print(concat_dataframe)
Following is the output of the above code ?
Class2 Physics_Marks Class1 Maths_Marks 0 Akash 79 Ram 95 1 Bhanu 85 Neha 80 2 krishna 96 John 90 3 Rani 80 Sam 89
Using merge() Method
The merge() method in the pandas module is used to concatenate data frames either vertically or horizontally. Based on common columns in the given dataframes it combines using a merge operation. The how parameter in merge() specifies the type of merge(inner, outer, left, or right), determining how the data frames are combined.
Example
In the following example, we have created two data frames and concatenated the first data frame to the second data frame using the merge() method ?
import pandas import numpy data_frame1 = pandas.DataFrame({'Course1': ["Python", "C++", "Java","Javasript"],'Price1': [15000, 20000, 30000, 35000]}) data_frame2 = pandas.DataFrame({'Course2': ["HTML", "CSS","C", "React"], 'Price2': [5000, 8000, 10000, 35000]}) concat_dataframe = data_frame2.join(data_frame1) print(concat_dataframe)
Following is the output of the above code ?
Course2 Price2 Course1 Price1 0 HTML 5000 Python 15000 1 CSS 8000 C++ 20000 2 C 10000 Java 30000 3 React 35000 Javasript 35000