How to concatenate two columns in a Pandas DataFrame?
Published on Aug. 22, 2023, 12:19 p.m.
To concatenate two columns in a Pandas DataFrame, you can use the +
operator or the pd.concat()
method. Here is an example using the +
operator:
import pandas as pd
# Create a sample DataFrame with two columns
data = {'first_name': ['John', 'Jane', 'Bob'], 'last_name': ['Doe', 'Smith', 'Johnson']}
df = pd.DataFrame(data)
# Concatenate the 'first_name' and 'last_name' columns
df['full_name'] = df['first_name'] + ' ' + df['last_name']
# Print the updated DataFrame
print(df)
This code will output the following DataFrame with a new ‘full_name’ column containing the concatenated values of the ‘first_name’ and ‘last_name’ columns:
first_name last_name full_name
0 John Doe John Doe
1 Jane Smith Jane Smith
2 Bob Johnson Bob Johnson
Alternatively, you can use the pd.concat()
method to concatenate the columns along the axis=1
direction:
df['full_name'] = pd.concat([df['first_name'], df['last_name']], axis=1).agg(' '.join, axis=1)
This will produce the same output as the previous method.
Note that you can also use the .str.cat()
method to concatenate columns. However, this method requires that both columns are of string type already.