如何将给定pandas数据帧行中的所有(字符串)值连接到一个字符串?

时间:2017-11-24 02:29:29

标签: python string pandas

我有一个像这样的pandas数据框:

     0        1            2        3       4
0    I        want         to       join    strings  
1    But      only         in       row     1

所需的输出应如下所示:

     0        1      2        3       4       5
1    But      only   in       row     1       I want to join strings

如何将这些字符串连接到联合字符串?

3 个答案:

答案 0 :(得分:2)

IIUC,使用applyjoin

df.apply(lambda x :' '.join(x.astype(str)),1)
Out[348]: 
0    I want to join strings
1         But only in row 1
dtype: object

然后你可以分配它们

df1=df.iloc[1:]
df1['5']=df.apply(lambda x :' '.join(x.astype(str)),1)[0]
df1
Out[361]: 
     0     1   2    3  4                       5
1  But  only  in  row  1  I want to join strings

时间安排:

%timeit df.apply(lambda x : x.str.cat(),1)
1 loop, best of 3: 759 ms per loop
%timeit df.apply(lambda x : ''.join(x),1)
1 loop, best of 3: 376 ms per loop


df.shape
Out[381]: (3000, 2000)

答案 1 :(得分:2)

使用str.cat加入第一行,然后分配给第二行。

i = df.iloc[1:].copy()   # the copy is needed to prevent chained assignment
i[df.shape[1]] = df.iloc[0].str.cat(sep=' ')

i     
     0     1   2    3  4                       5
1  But  only  in  row  1  I want to join strings

答案 2 :(得分:1)

另一种替代方式可以是add空格,后跟sum

df[5] = df.add(' ').sum(axis=1).shift(1)

结果:

     0     1   2     3        4                       5
0    I  want  to  join  strings                     NaN
1  But  only  in   row        1  I want to join strings