Python Pandas: merge a list of DataFrame

Jack Dong
3 min readJun 16, 2024

Merging a list of pandas DataFrames into a single DataFrame can be accomplished using various techniques, depending on your specific needs. Here are the most common approaches:

1. Using pd.concat()

The simplest way to merge a list of DataFrames is by concatenating them either vertically (row-wise) or horizontally (column-wise) using the pd.concat() function. This method is particularly useful when you want to stack DataFrames on top of each other or align them side by side.

Concatenate Vertically (Stacking DataFrames)

This method stacks DataFrames on top of each other, appending the rows of subsequent DataFrames below the previous ones.

import pandas as pd

# Assume dfs is a list of DataFrames
dfs = [df1, df2, df3] # These would be your actual DataFrame variables

# Concatenate them vertically
result = pd.concat(dfs, ignore_index=True)

print(result)

If your DataFrames have the same columns and you want to combine them into one DataFrame with the same column headers, ignore_index=True will reset the index in the resulting DataFrame. If you want to preserve the original indices, set it to False.

Concatenate Horizontally (Aligning DataFrames Side by Side)

--

--