eclairs eclairs - 2 months ago 10
Python Question

How to combine multiple rows of strings into one using pandas?

I have a DataFrame with multiple rows. Is there any way in which they can be combined to form one text?

For example:
words
0 I, will, hereby
1 am, gonna
2 going, far
3 to
4 do
5 this

Expected output:

I, will, hereby, am, gonna, going, far, to, do, this

Answer

You can use str.cat to join the strings in each row. For a Series or column s, write:

>>> s.str.cat(sep=', ')
'I, will, hereby, am, gonna, going, far, to, do, this'