-1

I would like to concatenate all columns in a pandas dataframe separated by spaces (" "). Is there a more pythonic way other than df['newcolumn'] = df['a'] + " " df['b'] + " " ...

a   b   c   combined
1   2   3   1 2 3
a   d   3   a d 3
p   0   k   p 0 k
magladde
  • 614
  • 5
  • 23

3 Answers3

2

lambda can be useful along axis=1

import pandas as pd
df = pd.DataFrame({'a':['1','a','p'], 
             'b':[2,'d',0],
             'c':[3,3,'k']})
df=df.astype(str)
df['combined']=df[df.columns].apply(lambda x: ' '.join(x), axis=1)
mad_
  • 8,121
  • 2
  • 25
  • 40
0

It will make the last column a list of all the values, but you can use the following:

df['combined'] = df.apply(lambda x: x.tolist(), axis=1)

The output will be as follows:

a   b   c   combined
1   2   3   [1,2,3]
a   d   3   [a,d,3]
p   0   k   [p,0,k]
atlas
  • 410
  • 4
  • 14
0

I don't think there is anything wrong with the way you are doing it, but a different way (perhaps more pythonic?) would be to:

df['newcolumn'] = df[['a', 'b', 'c']].apply(lambda x: ' '.join(x), axis=1)

to make it more generalizable to large df's:

df['newcolumn'] = df.iloc[:,0:3].apply(lambda x: ' '.join(x), axis=1) 

where the 0:3 in iloc is just column indexes [0,1,2]. You could do this for any arbitrary selection of columns by selecting the proper indexes.

d_kennetz
  • 5,219
  • 5
  • 21
  • 44
  • 1
    This is perfect, I have a dataframe that is 20 columns long so this last result really works wonders! – magladde Feb 09 '19 at 06:23