• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / Pandas DataFrame / Join Two Text Columns in Pandas / How to Join Two Text Columns into a Single Column in Pandas?

How to Join Two Text Columns into a Single Column in Pandas?

November 27, 2018 by cmdlinetips

Often one may want to join two text columns into a new column in a data frame. For example, one may want to combine two columns containing last name and first name into a single column with full name.
How to Join Two Text Columns in to One Column in Pandas?
How to Combine Two Text Columns in to One Column in Pandas?

We can use Pandas’ string manipulation functions to combine two text columns easily.
There are a few ways to combine two columns in Pandas. First we will see an example using cat function.

Let us first create a simple Pandas data frame using Pandas’ DataFrame function.

# import Pandas as pd
import pandas as pd
# create a new data frame
df = pd.DataFrame({'Last': ['Smith', 'Nadal', 'Federer'],
                   'First': ['Steve', 'Joe', 'Roger'],
                 'Age':[32,34,36]})
df

Here, we made a toy data frame with three columns and last name and first names are in two separate columns.

Age	First	Last
0	32	Steve	Smith
1	34	Joe	Nadal
2	36	Roger	Federer

How to Join Two Columns in Pandas with cat function

Let us use Python str function on first name and chain it with cat method and provide the last name as argument to cat function.

df['Name'] = df['First'].str.cat(df['Last'],sep=" ")
df

Now we have created a new column combining the first and last names.

	Age	First	Last	Name
0	32	Steve	Smith	Steve Smith
1	34	Joe	Nadal	Joe Nadal
2	36	Roger	Federer	Roger Federer

How to Combine Two Columns in Pandas with + operator

Another way to join two columns in Pandas is to simply use the + symbol. For example, to concatenate First Name column and Last Name column, we can do

df["Name"] = df["First"] + df["Last"]

We will get our results like this.

      Last  First  Age          Name
0    Smith  Steve   32    SteveSmith
1    Nadal    Joe   34      JoeNadal
2  Federer  Roger   36  RogerFederer

Note that there is no space between first and last name. To add any delimiter, we do

df["Name"] = df["First"] +" "+ df["Last"]

Now we get the Name column with the delimiter between first and last name as we wanted.

      Last  First  Age           Name
0    Smith  Steve   32    Steve Smith
1    Nadal    Joe   34      Joe Nadal
2  Federer  Roger   36  Roger Federer

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow to Collapse Multiple Columns in Pandas? Groupby with Dictionary Default ThumbnailHow to Split Text in a Column in Data Frame in R? Default ThumbnailHow to Split a Single Column in Pandas into Multiple Columns Default ThumbnailHow To Select Columns Using Prefix/Suffix of Column Names in Pandas?

Filed Under: Join Two Text Columns in Pandas, Python, String Manipulation in Python Tagged With: Join Two Text Columns in Pandas, Python Tips

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version