两个 pandas 列的字符串串联 [英] String concatenation of two pandas columns

查看:55
本文介绍了两个 pandas 列的字符串串联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下DataFrame:

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

它看起来像这样:

    bar foo
0    1   a
1    2   b
2    3   c

现在我想拥有类似的东西:

Now I want to have something like:

     bar
0    1 is a
1    2 is b
2    3 is c

我该如何实现? 我尝试了以下方法:

How can I achieve this? I tried the following:

df['foo'] = '%s is %s' % (df['bar'], df['foo'])

但是它给我一个错误的结果:

but it gives me a wrong result:

>>>print df.ix[0]

bar                                                    a
foo    0    a
1    b
2    c
Name: bar is 0    1
1    2
2
Name: 0

很抱歉,您遇到一个愚蠢的问题,但这是> pandas:将两列合并一个DataFrame 对我没有帮助.

Sorry for a dumb question, but this one pandas: combine two columns in a DataFrame wasn't helpful for me.

推荐答案

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

这篇关于两个 pandas 列的字符串串联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆