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 string columns of dataframe in 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 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 string columns of dataframe 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 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 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 String and numeric columns of the dataframe 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 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
Other Related Topics:
- Transpose the dataframe in pandas Python
- Get the data type of column in pandas python
- Strip Space in column of pandas dataframe (strip leading, trailing & all spaces of column in pandas)
- Get the substring of the column in pandas python
- join (merge) data frames in pandas
- Extract first n characters from left of column in pandas python
- Extract last n characters from right of the column in pandas python
for further details on string concatenation you can also refer here.