0

I have a dataframe with a variable called var1 and var2. There are 5 observations, as follows:

var1 var2

1  hello there
2  my name is
3  john and 
4  i am 30 years 
5  old 

is there a way to concatenate each observation string in var2 into one string? for example, create a string that concatenates the observations within a var: "hello there my name is john and i am 30 years old"

I looked everywhere online for a way to turn each observation for a particular var into one string, but could not find an example.

pynewbee
  • 665
  • 3
  • 9
  • 19

1 Answers1

2

Yes, use join:

s = ' '.join(df['var2'])
print (s)
hello there my name is john and i am 30 years old
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252