Df.info() is not displaying any result in st.write()

Hi,

I am trying to get information of pandas dataframe using the following code:

st.write(df.info())

But instead of printing information of dataframe it is printing “None” and the result is printed in command prompt instead.

Help me in resolving this issue.

Hi @anshul -

This occurs because this pandas function doesn’t return anything back to Python (which is why you get a value of None), it only prints. The pandas documentation for df.info() offers a similar type of solution:

Pipe output of DataFrame.info to buffer instead of sys.stdout, get buffer content and writes to a text file:

>>> import io 
>>> buffer = io.StringIO() 
>>> df.info(buf=buffer)
>>> s = buffer.getvalue() 
>>> with open("df_info.txt", "w", 
...     encoding="utf-8") as f:
...     f.write(s) 

In your case, you can probably do st.write(s) instead of writing to a file at the end.

1 Like