How to Append Multiple Pandas DataFrames (With Example)


You can use the following basic syntax to append multiple pandas DataFrames at once:

import pandas as pd

#append multiple DataFrames
df_big = pd.concat([df1,df2, df3], ignore_index=True) 

This particular syntax will append df1, df2, and df3 into a single pandas DataFrame called df_big.

The following example shows how to use this syntax in practice.

Example 1: Append Multiple Pandas DataFrames at Once

The following code shows how to append multiple pandas DataFrames at once:

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]})

#append all DataFrames into one DataFrame
df_big = pd.concat([df1,df2, df3], ignore_index=True)

#view resulting DataFrame
print(df_big)

        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 result is one big DataFrame that contains all of the rows from each of the three individual DataFrames.

The argument ignore_index=True tells pandas to ignore the original index numbers in each DataFrame and to create a new index that starts at 0 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
df_big = pd.concat([df1,df2])

#view resulting DataFrame
print(df_big)

        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.

In general, you should use ignore_index=True when appending multiple DataFrames unless you have a specific reason for keeping the original index values.

Additional Resources

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

Leave a Reply

Your email address will not be published. Required fields are marked *