Python Dataframe print column data as a continuous string

Question:

I am trying to print the column data of a data frame as a string.

code:

df = pd.DataFrame({'I_like':['carrots','banana','watermelon']})
df = 
       I_like
0     carrots
1      banana
2  watermelon

Expected output:

col_as_string= 
["carrots" or "banana" or "watermelon"]

Present code:

col_as_string = [ ""%s or""%(i) for i in df['I_like']]
col_as_string = ['"carrots or"', '"banana or"', '"watermelon or"']
Asked By: Mainland

||

Answers:

Try with:

print('"{}"'.format('" or "'.join(df['I_like'])))

Output:

'"carrots" or "banana" or "watermelon"'
Answered By: Quang Hoang
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.