Open In App

How to Stack Multiple Pandas DataFrames?

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Stacking multiple Pandas DataFrames means combining them either row-wise (vertically) or column-wise (horizontally) to form a single unified DataFrame.

For example, two DataFrames containing names like Brad and Leo and subjects like Math and Science can be combined into one DataFrame with merged rows and a continuous index.

Let’s explore simple and efficient ways to do this.

Using pd.concat()

pd.concat() function is the go-to method for combining DataFrames in Pandas. You can stack them vertically (row-wise) or horizontally (column-wise) by simply changing the axis parameter.

Example 1: Vertical Stacking (Row-wise)

Python
import pandas as pd

a = pd.DataFrame({'name': ['Brad', 'Leo'], 'subject': ['Math', 'Science']})
b = pd.DataFrame({'name': ['Christopher', 'Nolan'], 'subject': ['English', 'History']})

res = pd.concat([a,b], ignore_index=True)
print(res)

Output
          name  subject
0         Brad     Math
1          Leo  Science
2  Christopher  English
3        Nolan  History

Explanation: Two DataFrames a and b are created with names and subjects. pd.concat([a, b], ignore_index=True) stacks them vertically and resets the index, ensuring a continuous, clean sequence without retaining original index values.

Example 2: Horizontal Stacking (Column-wise)

Python
import pandas as pd
a = pd.DataFrame({'name': ['Chris', 'Bale'], 'subject': ['Math', 'Science']})
b = pd.DataFrame({'name': ['Magnus', 'Carlsan'], 'subject': ['English', 'History']})

res = pd.concat([a, b], axis=1)
print(res)

Output
    name  subject     name  subject
0  Chris     Math   Magnus  English
1   Bale  Science  Carlsan  History

Explanation: Two DataFrames with equal rows are combined side by side using axis=1, preserving row indices and duplicating column names if identical.

Using DataFrame.loc[]

This is helpful when you're collecting multiple DataFrames in a loop and want to combine them all later. It uses the same pd.concat() but works well with an iterable like a list of DataFrames.

Python
import pandas as pd
a = pd.DataFrame({'name': ['Chris', 'Bale'], 'subject': ['Math', 'Science']})
b = pd.DataFrame({'name': ['Magnus', 'Carlsan'], 'subject': ['English', 'History']})

frames = [a, b]
res = pd.concat(frames, ignore_index=True)

print(res)

Output
      name  subject
0    Chris     Math
1     Bale  Science
2   Magnus  English
3  Carlsan  History

Explanation: Multiple DataFrames are stored in a list and merged using pd.concat(). Setting ignore_index=True resets the index, making it ideal for combining DataFrames collected in a loop or iterable.

Using itertools.chain()

This is a powerful method when dealing with many small DataFrames or streamed data. It efficiently combines all row records from multiple DataFrames before creating the final one.

Python
from itertools import chain
import pandas as pd

a = pd.DataFrame({'name': ['Chris', 'Bale'], 'subject': ['Math', 'Science']})
b = pd.DataFrame({'name': ['Magnus', 'Carlsan'], 'subject': ['English', 'History']})

dfs = [a, b]
all_records = chain.from_iterable(df.to_dict('records') for df in dfs)

res = pd.DataFrame(all_records)
print(res)

Output
      name  subject
0    Chris     Math
1     Bale  Science
2   Magnus  English
3  Carlsan  History

Explanation: itertools.chain() flattens row records from multiple DataFrames, enabling efficient and memory-friendly merging into a single DataFrame.

Related articles


Practice Tags :

Similar Reads