How To Concatenate Two or More Pandas DataFrames?

Say you two dataframes of same size with same variables i.e. same columns and would like to concatenate them in to a single dataframe. Pandas’ concat() function can be handy in concatenating two or more such simple dataframes.

In this post, we will see examples of how to concatenate two dataframes into a single one. Let us load Pandas and NumPy.

# load pandas
import pandas as pd
# load numpy
import numpy as np
# check Pandas' version
pd.__version__
'1.0.0'

We will first create two data frames from scratch using numpy.

df1 = pd.DataFrame(np.random.randint(20, size=(2,3)),
                  index=list('ij'),
                   columns=list('ABC'))

Since it is a toy example, the first data frame we created has just two rows.

df1

	A	B	C
i	1	0	11
j	11	16	9

Similarly, we create a second data frame from scratch with two rows.

df2 = pd.DataFrame(np.random.randint(20, size=(2,3)),
                  index=list('mn'),
                   columns=list('ABC'))

We can verify that both the dataframes have same columns.

df2

        A	B	C
m	15	14	14
n	18	11	19

To concatenate the twp dataframes, we use concat() function with two dataframes as a list.

pd.concat([df1,df2])

        A	B	C
i	1	0	11
j	11	16	9
m	15	14	14
n	18	11	19

We can see that the two dataframes are concatenated the way we wanted.

This post is part of the series on Pandas 101, a tutorial covering tips and tricks on using Pandas for data munging and analysis.