1

I'd like to know if there is another way instead of using apply like this:

Given this DataFrame:

df = pd.DataFrame({1: {0: 'a', 1: 'a', 2: 'a'},
 2: {0: 'b', 1: 'b', 2: 'b'},
 3: {0: 'c', 1: 'c', 2: 'c'}})

Using this method:

df.apply(lambda x: ' '.join(x),axis=1)

Result:

s = pd.Series({0: 'a b c', 1: 'a b c', 2: 'a b c'})

Is there another way of doing this? I have a DataFrame with a lot of rows and I am trying to find the fastest way of doing such task.

Thank you!

ShayHa
  • 402
  • 1
  • 5
  • 16
  • 1
    You can try to convert it to series (of lists), and then use the vectorized str.join, kind of like this: `joined_ser = pd.Series(df.values.to_list()).str.join(' ')` – Oleg O Mar 03 '20 at 09:38
  • Why is the question closed? The given page doesn't hold an answer to this, as far as I can see. – Oleg O Mar 03 '20 at 09:48

0 Answers0