连接两个数值以使用pandas创建新列? [英] Concatenate two numerical values to make a new column using pandas?

查看:96
本文介绍了连接两个数值以使用pandas创建新列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据框中有两列.

var1    var2
01       001

我想创建将它们连接在一起的第三列:

I would like to create a third column that joins them together:

var1    var2    var3
01       001    01001

有人知道该怎么做吗?谢谢!

Does anyone know how to do this? Thank you!

推荐答案

您可以使用+进行简单的并发,而

You can use simple concanecate by + with casting by astype:

df['var3'] = df.var1.astype(str) + df.var2.astype(str)
print df
  var1 var2   var3
0   01  001  01001

如果两列的type均是string,则省略铸造:

If type of both columns is string casting is omited:

print type(df.loc[0,'var1'])
<type 'str'>
print type(df.loc[0,'var2'])
<type 'str'>

df['var3'] = df.var1 + df.var2
print df
  var1 var2   var3
0   01  001  01001

这篇关于连接两个数值以使用pandas创建新列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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