How To Concatenate Two or More Pandas DataFrames?

NISHANT TIWARI Last Updated : 06 Sep, 2024
7 min read

Introduction

Pandas is a powerful data manipulation library in Python that provides various functionalities for working with structured data. One of its critical features is its ability to handle and manipulate DataFrames, which are two-dimensional labelled data structures. In this article, we will explore the concept of concatenating DataFrames in Pandas and discuss its benefits and best practices.

In this article, you will learn how to concatenate dataframes in pandas using the pandas dataframe concat function, which enables you to pandas concat vertically two dataframes with ease.

Overview of Pandas DataFrames

DataFrames are tabular data structures in Pandas that consist of rows and columns. They are similar to tables in a relational database or spreadsheets. Each column in a DataFrame represents a different variable, while each row represents a specific observation or record. DataFrames provide a convenient way to organize, analyze, and manipulate data.

What is DataFrame Concatenation?

DataFrame concatenation refers to combining two or more DataFrames along a particular axis. It allows us to merge multiple data frames into a single data frame, vertically or horizontally. Concatenation is useful when we want to combine data from different sources or when we want to append new data to an existing DataFrame.

Concatenating DataFrames offers several benefits:

  • Consolidating data: Concatenation allows us to combine data from multiple sources into a single DataFrame, making it easier to analyze and manipulate the data.
  • Appending new data: We can use concatenation to add new rows or columns to an existing DataFrame, expanding its size and incorporating additional information.
  • Flexibility in data organization: Concatenation provides flexibility in organizing data. Based on our specific requirements, we can concatenate DataFrames vertically (along rows) or horizontally (along the columns).

Also Read: How to Use the Concatenate Function in Excel?

Concatenating DataFrames in Pandas

How To Concatenate Two or More Pandas DataFrames?

Using the `concat` Function

Pandas provides the `concat` function to concatenate DataFrames. The `concat` function takes a sequence of DataFrames as input and concatenates them along a specified axis. By default, it concatenates DataFrames vertically (along the rows).

Code:

import pandas as pd

df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

df2 = pd.DataFrame({'A': [7, 8, 9], 'B': [10, 11, 12]})

result = pd.concat([df1, df2])

print(result)

Output:

   A   B

0  1   4

1  2   5

2  3   6

0  7  10

1  8  11

2  9  12

Concatenating DataFrames with Different Columns

Sometimes, the Data Frames we want to concatenate may have different columns. Pandas handles this situation by aligning the columns based on their labels. If a column is missing in one data frame, Pandas fill it with null values.

Code:

import pandas as pd

df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

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

result = pd.concat([df1, df2])

print(result)

Output:

     A    B    C     D

0  1.0  4.0  NaN   NaN

1  2.0  5.0  NaN   NaN

2  3.0  6.0  NaN   NaN

0  NaN  NaN  7.0  10.0

1  NaN  NaN  8.0  11.0

2  NaN  NaN  9.0  12.0

Handling Duplicate Index Values

When concatenating DataFrames, duplicate index values can occur. Pandas provides options to handle this situation. We can either ignore the index or create a new index for the concatenated DataFrame.

Code:

import pandas as pd

df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}, index=[0, 1, 2])

df2 = pd.DataFrame({'A': [7, 8, 9], 'B': [10, 11, 12]}, index=[2, 3, 4])

result = pd.concat([df1, df2], ignore_index=True)

print(result)

Output:

   A   B

0  1   4

1  2   5

2  3   6

3  7  10

4  8  11

5  9  12

Concatenating DataFrames Horizontally

In addition to vertical concatenation, Pandas also allows us to concatenate DataFrames horizontally (along the columns). We can achieve this by specifying the `axis` parameter as 1.

Code:

import pandas as pd

df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

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

result = pd.concat([df1, df2], axis=1)

print(result)

Output:

   A  B  C   D

0  1  4  7  10

1  2  5  8  11

2  3  6  9  12

Concatenating DataFrames Vertically

By default, the `concat` function concatenates DataFrames vertically (along the rows). However, we can specify the `axis` parameter 0 to achieve the same result.

Code:

import pandas as pd

df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

df2 = pd.DataFrame({'A': [7, 8, 9], 'B': [10, 11, 12]})

result = pd.concat([df1, df2], axis=0)

print(result)

Output:

   A   B

0  1   4

1  2   5

2  3   6

0  7  10

1  8  11

2  9  12

Methods for DataFrame Combination

Merging DataFrames with the `merge` Function

In addition to concatenation, Pandas provides the `merge` function to combine DataFrames based on common columns or indexes. The `merge` function performs database-style joins, such as inner join, outer join, left join, and right join.

Code:

import pandas as pd

df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

df2 = pd.DataFrame({'A': [2, 3, 4], 'C': [7, 8, 9]})

result = pd.merge(df1, df2, on='A')

print(result)

Output:

   A  B  C

0  2  5  7

1  3  6  8

Joining DataFrames with the `join` function

Pandas’s `join` function allows us to combine DataFrames based on their indexes. It performs a left join by default, but we can specify different types of joins using the `how` parameter.

Code:

import pandas as pd

df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}, index=[0, 1, 2])

df2 = pd.DataFrame({'C': [7, 8, 9], 'D': [10, 11, 12]}, index=[2, 3, 4])

result = df1.join(df2)

print(result)

Output:

   A  B    C     D

0  1  4  NaN   NaN

1  2  5  NaN   NaN

2  3  6  7.0  10.0

Appending DataFrames with the `append` Function

Pandas’s `append` function allows us to append one DataFrame to another. It concatenates the rows of the second DataFrame to the end of the first DataFrame.

Code:

import pandas as pd

df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

df2 = pd.DataFrame({'A': [7, 8, 9], 'B': [10, 11, 12]})

result = df1.append(df2)

print(result)

Output:

   A   B

0  1   4

1  2   5

2  3   6

0  7  10

1  8  11

2  9  12

Best Practices for DataFrame Concatenation

Checking for Compatibility and Consistency

Before concatenating DataFrames, ensuring they are compatible and consistent is essential. This includes checking for the same number of columns, compatible data types, and consistent column names or indexes.

Handling Missing Data and Null Values

When concatenating DataFrames with different columns, missing data or null values are expected. Handling these missing values appropriately by filling them with default values or performing data imputation techniques is essential.

Managing Column Names and Indexes

Concatenating DataFrames may result in duplicate column names or indexes. Properly managing column names and indexes is recommended to avoid confusion and ensure data integrity. Renaming columns or resetting indexes can be helpful in such cases.

Avoiding Data Loss and Data Corruption

During the concatenation process, avoiding data loss or corruption is crucial. Creating a new DataFrame or copying the original DataFrames before concatenating them is recommended. This ensures the original data remains intact and any modifications are made on separate copies.

Examples and Use Cases

Concatenating DataFrames with Similar Structures

One everyday use case for concatenating DataFrames is when you have multiple DataFrames with similar structures and want to combine them into a single DataFrame. This can be useful when you have data split across multiple files or want to merge data from different sources.

Let’s say we have two DataFrames, df1 and df2, with the same columns, and we want to concatenate them vertically. We can use the `concat` function from the pandas library to achieve this:

Code:

import pandas as pd

df1 = pd.DataFrame({'A': [1, 2, 3],

                    'B': [4, 5, 6]})

df2 = pd.DataFrame({'A': [7, 8, 9],

                    'B': [10, 11, 12]})

result = pd.concat([df1, df2])

print(result)

Output:

   A   B

0  1   4

1  2   5

2  3   6

0  7  10

1  8  11

2  9  12

In this example, the `concat` function takes a list of DataFrames as its argument and concatenates them vertically. The resulting DataFrame contains all the rows from both df1 and df2.

Combining DataFrames with Different Columns

Another use case for concatenating DataFrames is when you have DataFrames with different columns and want to combine them horizontally. This can be useful when you want to add new columns to an existing DataFrame or when you want to merge data based on a standard column.

Let’s consider two DataFrames, df1 and df2, with different columns, and we want to concatenate them horizontally. We can use the `concat` function again, but this time we need to specify the `axis` parameter as 1 to indicate horizontal concatenation:

Code:

import pandas as pd

df1 = pd.DataFrame({'A': [1, 2, 3],

                    'B': [4, 5, 6]})

df2 = pd.DataFrame({'C': [7, 8, 9],

                    'D': [10, 11, 12]})

result = pd.concat([df1, df2], axis=1)

print(result)

Output:

   A  B  C   D

0  1  4  7  10

1  2  5  8  11

2  3  6  9  12

In this example, the `concat` function concatenates df1 and df2 horizontally, resulting in a DataFrame with all the columns from both DataFrames.

Concatenating Large DataFrames Efficiently

Concatenating large Data Frames can be computationally expensive and memory-intensive. You can use the `pd.concat` function to improve performance with the `ignore_index` parameter set to True. This will reset the index of the resulting Data Frame, avoiding the creation of a new index for each concatenated Data Frame.

Code:

import pandas as pd
df1 = pd.DataFrame({'A': [1, 2, 3],

                    'B': [4, 5, 6]})

df2 = pd.DataFrame({'A': [7, 8, 9],

                    'B': [10, 11, 12]})

result = pd.concat([df1, df2], ignore_index=True)

print(result)

Output:

   A   B

0  1   4

1  2   5

2  3   6

3  7  10

4  8  11

5  9  12

In this example, the resulting DataFrame has a new index that is generated based on the concatenation of df1 and df2, ignoring the original indices of each DataFrame. This can be particularly useful when dealing with large datasets where memory usage is a concern.

Conclusion

This article explored various techniques for concatenating Data Frames in pandas. We learned how to concatenate Data Frames with similar structures vertically and horizontally using the `concat` function. We also discussed handling Data Frames with different columns and concatenate large Data Frames efficiently.

Concatenating DataFrames is a powerful tool in pandas that allows us to combine data from different sources or split data across multiple files. It provides flexibility in handling data with similar or different structures and offers efficient ways to concatenate large datasets.

When concatenating DataFrames, it’s important to consider the data’s structure and the desired outcome. Understanding the available options and techniques can help us make informed decisions and achieve the expected results.

Hope you find this information helpful in learning how to concatenate dataframes in pandas using pandas dataframe concat to pandas concat vertically two dataframes!

Seasoned AI enthusiast with a deep passion for the ever-evolving world of artificial intelligence. With a sharp eye for detail and a knack for translating complex concepts into accessible language, we are at the forefront of AI updates for you. Having covered AI breakthroughs, new LLM model launches, and expert opinions, we deliver insightful and engaging content that keeps readers informed and intrigued. With a finger on the pulse of AI research and innovation, we bring a fresh perspective to the dynamic field, allowing readers to stay up-to-date on the latest developments.

Responses From Readers

Clear

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details