10.7 C
London
Sunday, July 7, 2024
HomePandas in PythonGeneral Functions in PythonHow to Stack Multiple Pandas DataFrames

How to Stack Multiple Pandas DataFrames

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

Often you may wish to stack two or more pandas DataFrames. Fortunately this is easy to do using the pandas concat() function.

This tutorial shows several examples of how to do so.

Example 1: Stack Two Pandas DataFrames

The following code shows how to “stack” two pandas DataFrames on top of each other and create one DataFrame:

import pandas as pd

#create two DataFrames
df1 = pd.DataFrame({'player': ['A', 'B', 'C', 'D', 'E'],
                    'points':[12, 5, 13, 17, 27]})

df2 = pd.DataFrame({'player': ['F', 'G', 'H', 'I', 'J'],
                    'points':[24, 26, 27, 27, 12]})

#"stack" the two DataFrames together
df3 = pd.concat([df1,df2], ignore_index=True)

#view resulting DataFrame
df3

	player	points
0	A	12
1	B	5
2	C	13
3	D	17
4	E	27
5	F	24
6	G	26
7	H	27
8	I	27
9	J	12

Example 2: Stack Three Pandas DataFrames

Similar code can be used to stack three pandas DataFrames on top of each other to create one DataFrame:

import pandas as pd

#create three DataFrames
df1 = pd.DataFrame({'player': ['A', 'B', 'C', 'D', 'E'],
                    'points':[12, 5, 13, 17, 27]})

df2 = pd.DataFrame({'player': ['F', 'G', 'H', 'I', 'J'],
                    'points':[24, 26, 27, 27, 12]})

df3 = pd.DataFrame({'player': ['K', 'L', 'M', 'N', 'O'],
                    'points':[9, 5, 5, 13, 17]})

#"stack" the two DataFrames together
df4 = pd.concat([df1,df2, df3], ignore_index=True)

#view resulting DataFrame
df4

        player	points
0	A	12
1	B	5
2	C	13
3	D	17
4	E	27
5	F	24
6	G	26
7	H	27
8	I	27
9	J	12
10	K	9
11	L	5
12	M	5
13	N	13
14	O	17

The Importance of ignore_index

Note that in the previous examples we used ignore_index=True.

This tells pandas to ignore the index numbers in each DataFrame and to create a new index ranging from 0 to n-1 for the new DataFrame.

For example, consider what happens when we don’t use ignore_index=True when stacking the following two DataFrames:

import pandas as pd

#create two DataFrames with indices
df1 = pd.DataFrame({'player': ['A', 'B', 'C', 'D', 'E'],
                    'points':[12, 5, 13, 17, 27]},
                    index=[0, 1, 2, 3, 4])

df2 = pd.DataFrame({'player': ['F', 'G', 'H', 'I', 'J'],
                    'points':[24, 26, 27, 27, 12]},
                    index=[2, 4, 5, 6, 9])

#stack the two DataFrames together
df3 = pd.concat([df1,df2])

#view resulting DataFrame
df3

        player	points
0	A	12
1	B	5
2	C	13
3	D	17
4	E	27
2	F	24
4	G	26
5	H	27
6	I	27
9	J	12

The resulting DataFrame kept its original index values from the two DataFrames.

Thus, you should typically use ignore_index=True when stacking two DataFrames unless you have a specific reason for keeping the original index values.

Additional Resources

The following tutorials explain how to perform other common tasks in Pandas:

How to Add an Empty Column to a Pandas DataFrame
How to Insert a Column Into a Pandas DataFrame
How to Export a Pandas DataFrame to Excel

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories