Home >Backend Development >Python Tutorial >How to Concatenate Two Columns in a Pandas DataFrame?

How to Concatenate Two Columns in a Pandas DataFrame?

Linda Hamilton
Linda HamiltonOriginal
2024-11-09 07:29:02319browse

How to Concatenate Two Columns in a Pandas DataFrame?

String Concatenation of Two Pandas Columns

Consider the following DataFrame:

from pandas import *
df = DataFrame({'foo':['a','b','c'], 'bar':[1, 2, 3]})

This DataFrame looks like:

bar foo
1 a
2 b
3 c

To concatenate the values from the 'foo' and 'bar' columns into a single column, use the following code:

df['bar'] = df.bar.map(str) + " is " + df.foo

The result is:

bar
1 is a
2 is b
3 is c

The above is the detailed content of How to Concatenate Two Columns in a Pandas DataFrame?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn