Home > Blockchain >  Python Dataframe print column data as a continuous string
Python Dataframe print column data as a continuous string

Time:09-03

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

CodePudding user response:

Try with:

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

Output:

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