2017-04-11 86 views

回答

0

將數字轉換爲str通過astype並添加字符串:

df['Col1'] = 'test-' + df['Col1'].astype(str) 
print (df) 

     Col1 Col2 Col3 
0 test-123 Name addr 
1 test-234 test first 

另一種解決方案:

df['Col1'] = df['Col1'].apply('test-{}'.format) 
print (df) 
     Col1 Col2 Col3 
0 test-123 Name addr 
1 test-234 test first 

時序

df = pd.concat([df]*10000).reset_index(drop=True) 

In [359]: %timeit 'test-' + df['Col1'].astype(str) 
10 loops, best of 3: 29.4 ms per loop 

In [360]: %timeit df['Col1'].apply('test-{}'.format) 
100 loops, best of 3: 6.66 ms per loop 
+0

收到這個錯誤 – mach

+0

類型錯誤:ufunc '添加' 不含有與簽名匹配類型的環路DTYPE( ' mach

+0

請檢查編輯答案。 – jezrael