How to Union Pandas DataFrames using Concat

The syntax to union pandas DataFrames using contact is:

 

pd.concat([df1, df2])

 

You may concatenate additional DataFrames by adding them within the brackets.

Next, I’m going to review an example with the steps to union pandas DataFrames using contact.

Steps to Union Pandas DataFrames using Concat

Step 1: Create the first DataFrame

For example, let’s say that you have the following data about your customers:

 

clientFirstNameclientLastNamecountry
JonSmithUS
MariaLamCanada
BruceJonesItaly
LiliChangChina

 

You can then create pandas DataFrame to capture that data in Python:

 

import pandas as pd

clients1 = {'clientFirstName': ['Jon','Maria','Bruce','Lili'],
            'clientLastName': ['Smith','Lam','Jones','Chang'],
            'country': ['US','Canada','Italy','China']
           }

df1 = pd.DataFrame(clients1, columns= ['clientFirstName', 'clientLastName','country'])

print (df1)

 

Run the code in Python and you would get:

 

Union Pandas DataFrames using Concat

Step 2: Create the second DataFrame

Now suppose that you got an additional data about new customers:

 

clientFirstNameclientLastNamecountry
BillJacksonUK
JackGreenGermany
ElizabethGrossBrazil
JennySingJapan

 

You can then create the second DataFrame as follows:

 

import pandas as pd

clients2 = {'clientFirstName': ['Bill','Jack','Elizabeth','Jenny'],
            'clientLastName': ['Jackson','Green','Gross','Sing'],
            'country': ['UK','Germany','Brazil','Japan']
           }

df2 = pd.DataFrame(clients2, columns= ['clientFirstName', 'clientLastName','country'])

print (df2)

 

Run the code, and you’ll see:

 

How to Union Pandas DataFrames

 

Your goal is to union those two DataFrames together. You can then use pandas concat to accomplish this goal.

Step 3: Union Pandas DataFrames using Concat

Finally, to union the two pandas DataFrames together, you can apply the generic syntax that we saw at the beginning of this tutorial:

 

pd.concat([df1, df2])

 

And here is the complete Python code to union pandas DataFrames using concat:

 

import pandas as pd

clients1 = {'clientFirstName': ['Jon','Maria','Bruce','Lili'],
            'clientLastName': ['Smith','Lam','Jones','Chang'],
            'country': ['US','Canada','Italy','China']
           }

df1 = pd.DataFrame(clients1, columns= ['clientFirstName', 'clientLastName','country'])


clients2 = {'clientFirstName': ['Bill','Jack','Elizabeth','Jenny'],
            'clientLastName': ['Jackson','Green','Gross','Sing'],
            'country': ['UK','Germany','Brazil','Japan']
           }

df2 = pd.DataFrame(clients2, columns= ['clientFirstName', 'clientLastName','country'])

union = pd.concat([df1, df2])
print (union)

 

Once you run the code, you’ll get the concatenated DataFrames:

 

How to Union Pandas DataFrames using Concat

 

One thing to notice is that the index values keep repeating (from 0 to 3 for the first DataFrame, and then from 0 to 3 for the second DataFrame):

 

 

You may then choose to assign the index values in an incremental manner once you concatenated the two DataFrames.

To do so, simply set ignore_index=True within the pd.concat brackets:

 

import pandas as pd

clients1 = {'clientFirstName': ['Jon','Maria','Bruce','Lili'],
            'clientLastName': ['Smith','Lam','Jones','Chang'],
            'country': ['US','Canada','Italy','China']
           }

df1 = pd.DataFrame(clients1, columns= ['clientFirstName', 'clientLastName','country'])


clients2 = {'clientFirstName': ['Bill','Jack','Elizabeth','Jenny'],
            'clientLastName': ['Jackson','Green','Gross','Sing'],
            'country': ['UK','Germany','Brazil','Japan']
           }

df2 = pd.DataFrame(clients2, columns= ['clientFirstName', 'clientLastName','country'])

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

 

And the result:

 

concat pandas

 

That’s it! The above method that we just reviewed would work even if you have more than 2 DataFrames. Note that you’ll need to keep the same column names across all the DataFrames to avoid any ‘NaN’ values.

For additional information about concatenating DataFrames, please visit the pandas.concat documentation.

You may also want to check the following tutorial that explains how to concatenate column values using pandas.