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
Release: 2024-11-09 07:29:02
Original
357 people have browsed it

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]})
Copy after login

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
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template