4

I am following this answer with 88 upvotes but it doesn't work anymore:

>>> df = pd.DataFrame(
     [['USA', 'Nevada', 'Las Vegas'],
      ['Brazil', 'Pernambuco', 'Recife']],
     columns=['Country', 'State', 'City']
 )
df['AllTogether'] = df['Country'].str.cat(df[['State', 'City']], sep=' - ')

I get the error:

    raise ValueError("Did you mean to supply a `sep` keyword?")
ValueError: Did you mean to supply a `sep` keyword?

Not sure what has changed since the posted answer.

I have pandas version of 0.22.0. I can't update the version due to test reasons.

Edit: I am not looking for alternate answer but the concern is about the error.

Zanam
  • 4,607
  • 13
  • 67
  • 143

1 Answers1

7

I was able to reproduce this error in pandas '0.21.1', but it appears to be a bug (which @Wen pointed out is now fixed in pandas 0.23.4). Regardless, you seem to be able to get around it by using df[['State', 'City']].values instead of just df[['State', 'City']], i.e by supplying a np.array instead of a dataframe (which is not the normal behaviour, seeing as the docs state you can pass it a dataframe):

df['AllTogether'] = df['Country'].str.cat(df[['State','City']].values,sep=' - ')
>>> df
  Country       State       City                  AllTogether
0     USA      Nevada  Las Vegas    USA - Nevada - Pernambuco
1  Brazil  Pernambuco     Recife  Brazil - Las Vegas - Recife
sacuL
  • 49,704
  • 8
  • 81
  • 106