0

I have a dataframe with different comments that were made and their characteristics. I have a column which is called "Free Text"

Basically, it contains for each comment the text corresponding to it.

Each of the rows contains a string, how can I paste all those strings together to get one single string?

I tried

data['text'].str.cat(sep=', ')

But I get TypeError: list indices must be integers, not str

Hannah Mse
  • 61
  • 1
  • 6
  • 2
    This error tells you, that data is a list. You only can access list items by their index like data[0], data[1].... Add some more of your code please. is data a list of dictionaries for the comments? – Igl3 Jul 24 '17 at 12:45
  • instead of doing .str try doing .__str__ or str(data['text']) and try – binu.py Jul 24 '17 at 12:47
  • Possible duplicate of [Python Pandas concatenate a Series of strings into one string](https://stackoverflow.com/questions/41400381/python-pandas-concatenate-a-series-of-strings-into-one-string) – zwer Jul 24 '17 at 12:50

1 Answers1

0

I think it should be like, it would work only in case data['text'].values results in string. if not you will have to convert that to string.

",".join(data['text'].values)

and here is a proper example

df = DataFrame(randn(5, 3), columns=list('ABC'), dtype=str)

print df['A'].values # prints all the values as list 
print ",".join(df['A'].values) # prints all the values as string seperated by , 
Gaurang Shah
  • 11,764
  • 9
  • 74
  • 137