0

I'm working on a chatbot that returns a string message to the user. However, my current code outputs a pandas DataFrame. May I know how to convert this DataFrame to a string output? Not just for the individual cell value. But the whole DataFrame change to string format.

such that type(df) is string.

df = 
     Bearish Neutral Bullish
CLOV   0.041   0.817   0.142
AMD    0.037   0.742   0.222
TTCF   0.063   0.759   0.177

type(df) =
<class 'pandas.core.frame.DataFrame'>
1

2 Answers 2

1

There are many ways to answer your question, depending on how you want the string to be formatted. Probably the simplest are df.to_csv() that creates a CSV-like table, and str(df) that formats the dataframe more or less the way it looks on your command line.

Sign up to request clarification or add additional context in comments.

Comments

0

If your goal is to output a string representation of the dataframe, you can use df.to_string().

You should get something like:

'     Bearish Neutral Bullish\nCLOV   0.041   0.817   0.142\nAMD    0.037   0.742   0.222\nTTCF   0.063   0.759   0.177'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.