深度阅读

How to concatenate two columns in a Pandas DataFrame?

作者
作者
2023年08月22日
更新时间
12.42 分钟
阅读时间
0
阅读量

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.

相关标签

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。