解决方案
sum,但输出是
float,因此必须转换为
int和
str:
df['new'] = df.sum(axis=1).astype(int).astype(str)
另一个具有
applyfunction的解决方案
join,但最慢:
df['new'] = df.apply(''.join, axis=1)最后非常快
numpy solution-转换为
numpyarray再“和”:
df['new'] = df.values.sum(axis=1)
时间 :
df = pd.Dataframe({'A': ['1', '2', '3'], 'B': ['4', '5', '6'], 'C': ['7', '8', '9']})#[30000 rows x 3 columns]df = pd.concat([df]*10000).reset_index(drop=True)#print (df)cols = list('ABC')#not_a_robot solutionIn [259]: %timeit df['concat'] = pd.Series(df[cols].fillna('').values.tolist()).str.join('')100 loops, best of 3: 17.4 ms per loopIn [260]: %timeit df['new'] = df[cols].astype(str).apply(''.join, axis=1)1 loop, best of 3: 386 ms per loopIn [261]: %timeit df['new1'] = df[cols].values.sum(axis=1)100 loops, best of 3: 6.5 ms per loopIn [262]: %timeit df['new2'] = df[cols].astype(str).sum(axis=1).astype(int).astype(str)10 loops, best of 3: 68.6 ms per loop编辑如果某些列的dtype不是
object(显然是
strings)强制转换
Dataframe.astype:
df['new'] = df.astype(str).values.sum(axis=1)



