Concatenate two columns of dataframe in pandas python

Concatenating two columns of the dataframe in pandas can be easily achieved by using simple ‘+’ operator. Concatenate or join of two string column in pandas python is accomplished by cat() function. we can also concatenate or join numeric and string column.  Let’s see how to

  • Concatenate two columns of dataframe in pandas (two string columns)
  • Concatenate integer (numeric) and string column of dataframe in pandas python
  • Concatenate column with space in pandas python
  • Concatenate columns with a delimiter (hyphen “-“) in pandas
  • Concatenate columns by removing leading and trailing space in pandas

Let’s first create the dataframe.


import pandas as pd
import numpy as np

#Create a DataFrame
df1 = {
    'State':['Arizona','Georgia','Newyork','Indiana','Florida'],
    'State_code':['AZ','GG','NY','IN','SL'],
   'Score':[62,47,55,74,31]}

df1 = pd.DataFrame(df1,columns=['State','State_code','Score'])
print(df1)

So the resultant dataframe will be

Concatenate two columns of dataframe in pandas python 1

 

Concatenate two string columns pandas: Method 1

Let’s concatenate two columns of dataframe with ‘+’ as shown below

df1['state_and_code'] = df1['State'] + df1['State_code']
print(df1)

So the result will be

Concatenate two columns of dataframe in pandas python 4

Concatenate two string columns pandas: Method 2 cat() Function

Let’s concatenate two columns of dataframe with cat() as shown below

df1['joined_col'] = df1.State.str.cat(df1.State_code)
print(df1)

So the result will be

Concatenate two columns of dataframe in pandas python 1x

 

 

Concatenate two string columns with space in pandas:

Let’s concatenate two columns of dataframe with space as shown below


df1['state_and_code'] = df1['State'] +' '+ df1['State_code']
print(df1)

So the result will be

Concatenate two columns of dataframe in pandas python 3

Concatenate two string columns with space in pandas:Method 2 using cat() function

Let’s concatenate two columns of dataframe with space using cat() function as shown below


df1['joined_col'] = df1.State.str.cat(df1.State_code,sep=" ")
print(df1)

So the result will be

Concatenate two columns of dataframe in pandas python 1y

 

 

 

Concatenate two string columns with – (hyphen) in pandas:

Let’s concatenate two columns of dataframe with – (hyphen) as shown below


df1['state_and_code'] = df1['State'] +'-'+ df1['State_code']
print(df1)

So the result will be

Concatenate two columns of dataframe in pandas python 2

 

 

 

Concatenate String and numeric column in pandas:

First we need to convert integer column to string column using map(str) function and then concatenate  as shown below


df1['code_and_score'] = df1["State_code"]+ "-" + df1["Score"].map(str)
print(df1)

So the resultant dataframe will be

Concatenate two columns of dataframe in pandas python 5

 

 

Concatenate columns by removing leading and trailing space in pandas

Let’s first concatenate two columns of dataframe with space using cat() function.Then we use strip() function to remove the leading and trailing space as shown below

df1['joined_col'] = df1.State.str.cat(df1.State_code,sep=" ")
df1['joined_col'] = df1['joined_col'].str.strip()
print (df1)

So the resultant dataframe with concatenated column and leading & trailing space removed will be

Concatenate two columns of dataframe in pandas python 1x

Concatenate two columns of dataframe in pandas python                                                                                                           Concatenate two columns of dataframe in pandas python