Home >Backend Development >Python Tutorial >How to Concatenate Two Columns in a Pandas DataFrame?
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!