Python 3.x 在df列中的str上使用str.join
这里有没有使用join的方法Python 3.x 在df列中的str上使用str.join,python-3.x,pandas,string,dataframe,join,Python 3.x,Pandas,String,Dataframe,Join,这里有没有使用join的方法 df = pd.DataFrame([ ['a','b','c'] ]) df['combined'] = df[0] + ':' + df[1] + ':' + df[2] df 这就是我尝试过的: df['combined'] = ':'.join([df[0], df[1], df[2]]) 第一种方法是最有效的,但您可以通过cat使用.str访问 df['combined']=df[0].str.cat(df.loc[:, 1:], sep=':
df = pd.DataFrame([
['a','b','c']
])
df['combined'] = df[0] + ':' + df[1] + ':' + df[2]
df
这就是我尝试过的:
df['combined'] = ':'.join([df[0], df[1], df[2]])
第一种方法是最有效的,但您可以通过cat使用.str访问
df['combined']=df[0].str.cat(df.loc[:, 1:], sep=':')
print(df)
输出:
0 1 2 combined
0 a b c a:b:c
其他效率较低的方法:
df.apply(lambda x: ':'.join(x) ,axis=1)