Python Dataframe print column data as a continuous string

Viewed 28

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"']
1 Answers

Try with:

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

Output:

'"carrots" or "banana" or "watermelon"'
Related