pandas :组合字符串和int列 [英] Pandas: Combine string and int columns

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

问题描述

我有一个以下 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

对不起,一个愚蠢的问题,但这一个大熊猫:组合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 :组合字符串和int列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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