The Wayback Machine - https://web.archive.org/web/20240118171215/https://www.geeksforgeeks.org/join-two-text-columns-into-a-single-column-in-pandas/
Open In App
Related Articles

Join two text columns into a single column in Pandas

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Let’s see the different methods to join two text columns into a single column.

Method #1: Using cat() function
We can also use different separators during join. e.g. -, _, ” ” etc.




# importing pandas
import pandas as pd
  
df = pd.DataFrame({'Last': ['Gaitonde', 'Singh', 'Mathur'],
                   'First': ['Ganesh', 'Sartaj', 'Anjali']})
  
print('Before Join')
print(df, '\n')
  
print('After join')
df['Name'] = df['First'].str.cat(df['Last'], sep =" ")
print(df)


Output :

 
Method #2: Using lambda function

This method generalizes to an arbitrary number of string columns by replacing df[[‘First’, ‘Last’]] with any column slice of your dataframe, e.g. df.iloc[:, 0:2].apply(lambda x: ‘ ‘.join(x), axis=1).




# importing pandas
import pandas as pd
  
df = pd.DataFrame({'Last': ['Gaitonde', 'Singh', 'Mathur'],
                   'First': ['Ganesh', 'Sartaj', 'Anjali']})
  
print('Before Join')
print(df, '\n')
  
print('After join')
df['Name'] = df[['First', 'Last']].apply(lambda x: ' '.join(x), axis = 1)
print(df)


Output :

Method #3: Using + operator

We need to convert data frame elements into string before join. We can also use different separators during join, e.g. -, _, ‘ ‘ etc.




# importing pandas
import pandas as pd
  
df = pd.DataFrame({'Last': ['Gaitonde', 'Singh', 'Mathur'],
                   'First': ['Ganesh', 'Sartaj', 'Anjali']})
  
print('Before Join')
print(df, '\n')
  
print('After join')
df['Name']= df["First"].astype(str) +" "+ df["Last"]
print(df)


Output :


Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 01 Jan, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials