How to Stack Multiple Pandas DataFrames?
Last Updated :
11 Jul, 2025
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.
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
Similar Reads
How to read multiple data files into Pandas? In this article, we are going to see how to read multiple data files into pandas, data files are of multiple types, here are a few ways to read multiple files by using the pandas package in python. The demonstrative files can be download from here Method 1: Reading CSV files If our data files are in
3 min read
How to Write Pandas DataFrames to Multiple Excel Sheets? Writing multiple Pandas DataFrames to different sheets in an Excel file is a common task when organizing structured data. For example, if you have separate datasets like sales reports, inventory logs and customer feedback, you might want each to appear on a different sheet within a single Excel file
3 min read
How to Write Pandas DataFrames to Multiple Excel Sheets? Writing multiple Pandas DataFrames to different sheets in an Excel file is a common task when organizing structured data. For example, if you have separate datasets like sales reports, inventory logs and customer feedback, you might want each to appear on a different sheet within a single Excel file
3 min read
Merge Multiple Dataframes - Pandas Merging allow us to combine data from two or more DataFrames into one based on index values. This is used when we want to bring together related information from different sources. In Pandas there are different ways to combine DataFrames:1. Merging DataFrames Using merge()We use merge() when we want
3 min read
Merge Multiple Dataframes - Pandas Merging allow us to combine data from two or more DataFrames into one based on index values. This is used when we want to bring together related information from different sources. In Pandas there are different ways to combine DataFrames:1. Merging DataFrames Using merge()We use merge() when we want
3 min read
How to Plot Multiple Series from a Pandas DataFrame? In this article, we will discuss how to plot multiple series from a dataframe in pandas. Series is the range of the data  that include integer points we cab plot in pandas dataframe by using plot() function Syntax: matplotlib.pyplot(dataframe['column_name']) We can place n number of series and we ha
2 min read