How can I append two Pandas DataFrames together?

How can I append two Pandas DataFrames together?

Appending two Pandas DataFrames is a process of combining two separate data structures into one cohesive DataFrame. This can be achieved by using the “append” function in Pandas, which allows for the addition of rows from one DataFrame to the end of another DataFrame. The DataFrames must have the same column names in order for the append function to work properly. This method is useful for organizing and manipulating large amounts of data, as well as merging data from different sources. Overall, appending DataFrames in Pandas is a simple and efficient way to merge data and create a comprehensive dataset.

Append Two Pandas DataFrames (With Examples)


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 .

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

Cite this article

stats writer (2024). How can I append two Pandas DataFrames together?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-append-two-pandas-dataframes-together/

stats writer. "How can I append two Pandas DataFrames together?." PSYCHOLOGICAL SCALES, 4 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-append-two-pandas-dataframes-together/.

stats writer. "How can I append two Pandas DataFrames together?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-append-two-pandas-dataframes-together/.

stats writer (2024) 'How can I append two Pandas DataFrames together?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-append-two-pandas-dataframes-together/.

[1] stats writer, "How can I append two Pandas DataFrames together?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I append two Pandas DataFrames together?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top