How to Concatenate DataFrames in Python?

Estimated read time 2 min read

To concatenate DataFrames in Python, you can use the pd.concat() function from the pandas library. This function allows you to combine multiple DataFrames along a specified axis. Here’s an example:

import pandas as pd

# Create sample DataFrames
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df2 = pd.DataFrame({'A': [7, 8, 9], 'B': [10, 11, 12]})

# Concatenate the DataFrames vertically (along the rows)
concatenated_df = pd.concat([df1, df2], axis=0)

# Concatenate the DataFrames horizontally (along the columns)
concatenated_df_horizontal = pd.concat([df1, df2], axis=1)

print("Vertically concatenated DataFrame:")
print("\nHorizontally concatenated DataFrame:")

In this example, we have two sample DataFrames, df1 and df2, with the same column names (‘A’ and ‘B’).

To concatenate them vertically, we use pd.concat() with axis=0, which is the default value. This creates a new DataFrame concatenated_df where df2 is appended below df1.

To concatenate them horizontally, we use pd.concat() with axis=1. This creates a new DataFrame concatenated_df_horizontal where the columns of df2 are added as new columns next to df1.

The output will be:

Vertically concatenated DataFrame:
   A   B
0  1   4
1  2   5
2  3   6
0  7  10
1   8  11
2   9  12

Horizontally concatenated DataFrame:
   A  B  A   B
0  1  4  7  10
1  2  5  8  11
2  3  6  9  12

As you can see, pd.concat() combines the DataFrames based on the specified axis, creating a new DataFrame with the concatenated data.

You can pass a list of DataFrames to pd.concat() to concatenate more than two DataFrames. Additionally, you can customize the behavior of pd.concat() using parameters such as join, ignore_index, and keys to control the index alignment, handling of duplicate indices, and adding hierarchical indexing, respectively.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply