How to Combine Two Text or String Columns in Pandas

Spread the love

Problem –

You have two text columns and you want to combine both of them into one column.

Solution –

Let’s read a dataset to work with.

import pandas as pd

url = 'https://raw.githubusercontent.com/bprasad26/lwd/master/data/clothing_store_sales.csv'
df = pd.read_csv(url)
df.head()

Let’s say that you want to combine Gender and Marital Status columns. Since Both the columns are of string type we can combine them directly like this.

df['Gender_Marital_Status'] = df['Gender'] + df['Marital Status']

Now, suppose you want to combine the Gender and Age columns. Here one column is a text column and the another is a numerical column. To combine any column that is not string type, you have to first convert them into a string type and then combine both the columns.

df['Gender_Age'] = df['Gender'] + df['Age'].astype(str)

And if you want to join multiple string columns into one, you can use the agg method.

Let’s say you want to join Type of customer, Gender and Marital status together. To do that you have to write.

df['Type_Gender_Marital'] = df[['Type of Customer','Gender','Marital Status']].agg('-'.join, axis=1)

Here “-” is a separator. If you want you can use other separators like a white space.

df['Type_Gender_Marital'] = df[['Type of Customer','Gender','Marital Status']].agg(' '.join, axis=1)

Rating: 1 out of 5.

Leave a Reply