HOWTO · Pandas

How to Combine Two Columns of Text in DataFrame in Pandas

combine columns in DataFrame using '+' operator, apply(), map(), str.cat(), agg() methods in pandas library

On this page

Combining two text columns in a Pandas DataFrame is straightforward: add the two Series with a space between them, then assign the result to a new column.
This keeps each first-and-last pair aligned with its original row.

import pandas as pd

data = [["Ali", "Azmat", "30"], ["Sharukh", "Khan", "40"], ["Linus", "Torvalds", "70"]]
df = pd.DataFrame(data, columns=["First", "Last", "Age"])
df["Full Name"] = df["First"] + " " + df["Last"]
print(df)
     First      Last Age       Full Name
0      Ali     Azmat  30       Ali Azmat
1  Sharukh      Khan  40    Sharukh Khan
2    Linus  Torvalds  70  Linus Torvalds

The example produces the Full Name column shown below. The five methods use the same input and produce the same values. Choose + or Series.str.cat() for direct string concatenation; use the row-wise methods when a row function makes the transformation easier to express.

+ Operator Method

For string columns, + concatenates values row by row. The separator is an ordinary string, so change " " to ", " or another delimiter to match the desired format. The operator also supports numeric addition, so matching data types alone does not make it a text-concatenation operation: use it here because First and Last contain strings.

import pandas as pd

data = [["Ali", "Azmat", "30"], ["Sharukh", "Khan", "40"], ["Linus", "Torvalds", "70"]]
df = pd.DataFrame(data, columns=["First", "Last", "Age"])
df["Full Name"] = df["First"] + " " + df["Last"]
print(df)

The resulting DataFrame is:

     First      Last Age       Full Name
0      Ali     Azmat  30       Ali Azmat
1  Sharukh      Khan  40    Sharukh Khan
2    Linus  Torvalds  70  Linus Torvalds

Series.map() Method

Series.map() applies a mapping to each value in one Series. It is not a DataFrame.map() call, and converting these already-string names with str is unnecessary; the example retains that conversion to show how the mapped Series can be combined with another column. For mixed values, decide explicitly how missing values and non-string values should be represented before converting them.

import pandas as pd

data = [["Ali", "Azmat", "30"], ["Sharukh", "Khan", "40"], ["Linus", "Torvalds", "70"]]
df = pd.DataFrame(data, columns=["First", "Last", "Age"])
df["Full Name"] = df["First"].map(str) + " " + df["Last"]
print(df)

The resulting DataFrame is:

     First      Last Age       Full Name
0      Ali     Azmat  30       Ali Azmat
1  Sharukh      Khan  40    Sharukh Khan
2    Linus  Torvalds  70  Linus Torvalds

df.apply() Method

apply can call a function once for each row when you pass axis=1. Selecting only First and Last keeps Age out of the join. The string join operation expects each selected value to be a string; convert or handle missing and non-string values deliberately before using it on less uniform data. For a simple separator between two text columns, + or Series.str.cat() is usually more direct.

import pandas as pd

data = [["Ali", "Azmat", "30"], ["Sharukh", "Khan", "40"], ["Linus", "Torvalds", "70"]]
df = pd.DataFrame(data, columns=["First", "Last", "Age"])
df["Full Name"] = df[["First", "Last"]].apply(" ".join, axis=1)
print(df)

The resulting DataFrame is:

     First      Last Age       Full Name
0      Ali     Azmat  30       Ali Azmat
1  Sharukh      Khan  40    Sharukh Khan
2    Linus  Torvalds  70  Linus Torvalds

Series.str.cat() Method

Series.str.cat() is designed for concatenating string values and makes the separator explicit with sep. By default, a missing value in either Series makes that row’s result missing. Set na_rep when you want a replacement for missing values, and choose that replacement to fit the meaning of your data.

import pandas as pd

data = [["Ali", "Azmat", "30"], ["Sharukh", "Khan", "40"], ["Linus", "Torvalds", "70"]]
df = pd.DataFrame(data, columns=["First", "Last", "Age"])
df["Full Name"] = df["First"].str.cat(df["Last"], sep=" ")
print(df)

The resulting DataFrame is:

     First      Last Age       Full Name
0      Ali     Azmat  30       Ali Azmat
1  Sharukh      Khan  40    Sharukh Khan
2    Linus  Torvalds  70  Linus Torvalds

df.agg() Method

agg can also apply a row-wise join when given axis=1 and a function such as " ".join. Like the apply example, it operates on the two selected columns and expects their values to be strings. Use it when aggregation is already part of your row-wise processing; for this simple task, the + operator or Series.str.cat() communicates the intent more directly. Check the resulting Full Name column and a few rows when adapting the code to your own data.

import pandas as pd

data = [["Ali", "Azmat", "30"], ["Sharukh", "Khan", "40"], ["Linus", "Torvalds", "70"]]
df = pd.DataFrame(data, columns=["First", "Last", "Age"])
df["Full Name"] = df[["First", "Last"]].agg(" ".join, axis=1)
print(df)

The resulting DataFrame is:

     First      Last Age       Full Name
0      Ali     Azmat  30       Ali Azmat
1  Sharukh      Khan  40    Sharukh Khan
2    Linus  Torvalds  70  Linus Torvalds