我有以下数据框
Col1 Col2 Col3
123 name test
234 rest check
我要求发生以下事情
Col1 Col2 Col3
test-123 name test
test-234 rest check
将“test-
”添加到数据框的Col1
答案 0 :(得分:0)
按astype
将数字转换为str
并添加字符串:
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