2016-04-21 105 views

回答

2

您可以通过+与铸造用astype使用简单concanecate:

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

如果两列typestring铸件被遗漏:

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 
0

转换为两列,将字符串,然后加入两列都形成第三列。

代码:

df['var1']=df['var1'].astype(str) 
df['var2']=df['var2'].astype(str) 
df['var3'] = df[['var1', 'var2']].apply(lambda x: ''.join(x), axis=1)