Concatenate rows from multiple Pandas DataFrames

Python

Pandas

Join

Concatenate rows

Luc B.

Python

Pandas

DataFrame

illustration of row concatenation

As shown above, concatenating DataFrame rows is a simple operation to vertically join DataFrames. Potential applications include combining structurally consistent data from multiple sources into a single DataFrame.

Code Example

Use the pd.concat() function to concatenate rows from different DataFrames.

import pandas as pd

df1 = pd.DataFrame({
    'column1': [1, 2, 3, 4]
})

df2 = pd.DataFrame({
    'column1': [5, 6, 7, 8]
})

df3 = pd.DataFrame({
    'column1': [5, 6, 7, 8]
})

# Concatenate rows here
pd.concat([df1, df2, df3])
column1
0 1
1 2
2 3
3 4
0 5
1 6
2 7
3 8
0 5
1 6
2 7
3 8

Note in the example above, the resulting DataFrame preserves the index values of the original DataFrames. However, by using the ignore_index parameter, Pandas will assign new index values to the resulting DataFrame that range from 0 to n.

# Concatenate rows and create new unique index values
pd.concat([df1, df2, df3], ignore_index=True)
column1
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 5
9 6
10 7
11 8

This example demonstrates row-wise concatenation. If you want to see column-wise concatenation, check out this article.