Home » How to Append Two Pandas DataFrames (With Examples)

How to Append Two Pandas DataFrames (With Examples)

by Tutor Aspire

You can use the following basic syntax to append two pandas DataFrames into one DataFrame:

big_df = pd.concat([df1, df2], ignore_index=True)

The following examples show how to use this syntax in practice.

Example 1: Append Two Pandas DataFrames

The following code shows how to append two pandas DataFrames together into one DataFrame:

import pandas as pd

#create two DataFrames
df1 = pd.DataFrame({'x': [25, 14, 16, 27, 20, 12, 15, 14, 19],
                    'y': [5, 7, 7, 5, 7, 6, 9, 9, 5],
                    'z': [8, 8, 10, 6, 6, 9, 6, 9, 7]})

df2 = pd.DataFrame({'x': [58, 60, 65],
                    'y': [14, 22, 23],
                    'z': [9, 12, 19]})

#append two DataFrames together
combined = pd.concat([df1, df2], ignore_index=True)

#view final DataFrame
combined

	x	y	z
0	25	5	8
1	14	7	8
2	16	7	10
3	27	5	6
4	20	7	6
5	12	6	9
6	15	9	6
7	14	9	9
8	19	5	7
9	58	14	9
10	60	22	12
11	65	23	19

Example 2: Append More Than Two Pandas DataFrames

Note that you can use the pd.concat() function to append more than two pandas DataFrames together:

import pandas as pd

#create three DataFrames
df1 = pd.DataFrame({'x': [25, 14, 16],
                    'y': [5, 7, 7]})

df2 = pd.DataFrame({'x': [58, 60, 65],
                    'y': [14, 22, 23]})

df3 = pd.DataFrame({'x': [58, 61, 77],
                    'y': [10, 12, 19]})

#append all three DataFrames together
combined = pd.concat([df1, df2, df3], ignore_index=True)

#view final DataFrame
combined

	x	y
0	25	5
1	14	7
2	16	7
3	58	14
4	60	22
5	65	23
6	58	10
7	61	12
8	77	19

Note that if we didn’t use the ignore_index argument, the index of the resulting DataFrame would retain the original index values for each individual DataFrame:

#append all three DataFrames together
combined = pd.concat([df1, df2, df3])

#view final DataFrame
combined

	x	y
0	25	5
1	14	7
2	16	7
0	58	14
1	60	22
2	65	23
0	58	10
1	61	12
2	77	19

You can find the complete online documentation for the pandas.concat() function here.

Additional Resources

The following tutorials explain how to perform other common functions in pandas:

How to Use Pandas fillna() to Replace NaN Values
How to Merge Pandas DataFrames on Multiple Columns
How to Merge Two Pandas DataFrames on Index

You may also like