0

I have 2 columns in a df: Name and Year and i want to concatenate them.

According to this answer the best solution (and the fastest in my experience as well) is:

df[['Year','quarter']].astype(str).sum(axis=1)

This results in something like:

Adam2013
John2014

What i would need is:

Adam 2013
John 2014

Meaning that i would need them separated by ' '.

Is there a way to do this without compromision on speed? Thanks!

EDIT:

I did find a solution but i don't regard it as pretty:

Create an artificial column:

df['separator'] = ' '

Merge the columns with the separators in place:

df[['Year','separator','quarter']].astype(str).sum(axis=1)

Get rid of the separator column:

df.drop(['separator'], axis = 1, inplace = True)
Mike Vlad
  • 351
  • 1
  • 4
  • 12

0 Answers0