Concatenate columns from multiple Pandas DataFrames

Python

Pandas

Join

Concatenate columns

Luc B.

Python

Pandas

DataFrame

illustration of column concatenation

As shown above, concatenating DataFrame columns is a simple operation to horizontally join DataFrames. This operation can be used to combine structutally-consistent data from multiple sources into a single DataFrame.

Code Example

Use the pd.concat() function to append the columns of one DataFrame to another. The axis parameter must be set to 1 to ensure the concatenation is column-wise and not row-wise.

import pandas as pd

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

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

# Concatenate columns here
pd.concat([df1, df2], axis=1)
column1 column2
0 1 5
1 2 6
2 3 7
3 4 8

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