Concatenate Column Values in Python using Pandas

In this guide, I’ll show you how to concatenate column values in Python using pandas.

But before we begin, here is the generic structure that you may use to perform the concatenation using pandas DataFrame:

 

df1 = df['1st Column Name'] + df['2nd Column Name'] + ...

 

Notice that we used the plus symbol (‘+’) in order to concatenate values.

Also note that if your data-set contains numbers, and you are trying to use the above structure, you may get this error:

‘TypeError: ufunc ‘add’ did not contain a loop with signature matching types dtype(‘<U32’) dtype(‘<U32’) dtype(‘<U32′)’ 

You can bypass this error by mapping the values to strings using this structure:

 

df1 = df['1st Column Name'].map(str) + df['2nd Column Name'].map(str) + ...

 

Next, we are going to review the following 3 examples to see how to concatenate column values in Python:

  • Example-1: concatenating values using a single DataFrame
  • Example-2: concatenating two DataFrames
  • Example-3: concatenating two DataFrames, and then finding the Max value

Example-1: concatenating values using a single DataFrame

To start, let’s say that you have the following data which contains 3 columns: Day, Month and Year.

DayMonthYear
1Jun2016
2Jul2017
3Aug2018
4Sep2019
5Oct2020

Your goal is to concatenate the column values in Python as follows:

Day-Month-Year

In the next section, I’ll review the steps needed to perform this type of concatenation.

Steps to concatenate column values in Python

(1) Firstly, you’ll need to install the pandas package (if you haven’t already done so). You can check this tutorial that explains how to install packages in Python using PIP

(2) Then, you’ll need to create the DataFrame based on the values in our example. You may use the following Python code to create the DataFrame:

 

from pandas import DataFrame 

Dates = {'Day': [1,2,3,4,5], 
        'Month': ['Jun','Jul','Aug','Sep','Oct'], 
        'Year': [2016,2017,2018,2019,2020]} 

df = DataFrame(Dates, columns= ['Day', 'Month','Year']) 

 

(3) Finally, apply the following syntax to perform the concatenation in Python:

 

df = df['Day'].map(str) + '-' + df['Month'].map(str) + '-' + df['Year'].map(str)

 

And your full code should look like this:

 

from pandas import DataFrame 

Dates = {'Day': [1,2,3,4,5], 
        'Month': ['Jun','Jul','Aug','Sep','Oct'], 
        'Year': [2016,2017,2018,2019,2020]} 

df = DataFrame(Dates, columns= ['Day', 'Month','Year']) 

df1 = df['Day'].map(str) + '-' + df['Month'].map(str) + '-' + df['Year'].map(str)

print (df1)

 

Press F5 to run the code and you’ll get this result:

 

Concatenate Column Values in Python using Pandas

Example-2: concatenating two DataFrames

Now we’ll see how to concatenate the column values from two separate DataFrames.

In the previous example, we saw how to create the first DataFrame based on this data:

DayMonthYear
1Jun2016
2Jul2017
3Aug2018
4Sep2019
5Oct2020

We will now create a second DataFrame based on the below data:

Unemployment RateInterest Rate
5.51.75
51.5
5.21.25
5.11.5
4.92

The goal is to concatenate the values from the two DataFrames as follows:

Day-Month-Year: Unemployment Rate;  Interest Rate

To accomplish our goal, you may apply the following Python code:

 

from pandas import DataFrame 

Dates = {'Day': [1,2,3,4,5], 
        'Month': ['Jun','Jul','Aug','Sep','Oct'], 
        'Year': [2016,2017,2018,2019,2020]} 

df1 = DataFrame(Dates, columns= ['Day', 'Month','Year']) 


Rates = {'Unemployment Rate': [5.5,5,5.2,5.1,4.9], 
         'Interest Rate': [1.75,1.5,1.25,1.5,2]} 

df2 = DataFrame(Rates, columns= ['Unemployment Rate', 'Interest Rate'])


df_combined = df1['Day'].map(str) + '-' + df1['Month'].map(str) + '-' + df1['Year'].map(str) + ': ' + 'Unemployment: ' + df2['Unemployment Rate'].map(str) + '; ' + 'Interest: ' + df2['Interest Rate'].map(str)

print (df_combined)

 

And once your run the Python code, you’ll get the following result:

 

Concatenating two DataFrames in Python

Example-3: concatenating two DataFrames, and then finding the Max value

In the third, and last example, we are going to concatenate the 2 DataFrames below (which would contain only numbers), and then find the Max value.

The purpose of this exercise is to demonstrate that you can apply different arithmetic/statistical operations once you concatenated the 2 separate DataFrames.

The 1st DataFrame would contain this set of numbers:

 

Set1 = {'Set1': [55,22,11,77,33]} 
df1 = DataFrame(Set1, columns= ['Set1']) 

 

While the 2nd DataFrame would contain this set of numbers:

 

Set2 = {'Set2': [23,45,21,73,48]} 
df2 = DataFrame(Set2, columns= ['Set2'])

 

You can then concatenate these 2 DataFrames, and then find the Max value by using this code:

 

from pandas import DataFrame 

Set1 = {'Set1': [55,22,11,77,33]} 
df1 = DataFrame(Set1, columns= ['Set1']) 

Set2 = {'Set2': [23,45,21,73,48]} 
df2 = DataFrame(Set2, columns= ['Set2'])

Concatenated = df1['Set1'].map(str) + df2['Set2'].map(str)

df_combined = DataFrame(Concatenated, columns=['Combined Values'])
max1 = df_combined['Combined Values'].max()

print (max1)

 

And the result that you’ll get is ‘7773,’ which is indeed the maximum value.

To learn more about pandas DataFrame, you may check the pandas documentation.