2016-04-21 102 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)