jam*_*jam 8 python concatenation pandas
我的数据框中有两列。
var1 var2
01 001
Run Code Online (Sandbox Code Playgroud)
我想创建将它们连接在一起的第三列:
var1 var2 var3
01 001 01001
Run Code Online (Sandbox Code Playgroud)
有谁知道如何做到这一点?谢谢!
jez*_*ael 10
您可以使用简单的 concatenate by+和 cast by astype:
df['var3'] = df.var1.astype(str) + df.var2.astype(str)
print df
var1 var2 var3
0 01 001 01001
Run Code Online (Sandbox Code Playgroud)
如果省略type了两列的string强制转换:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10213 次 |
| 最近记录: |