How to return a string from pandas.DataFrame.info()
<p>I would like to display the output from <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.info.html" rel="nofollow"><code>pandas.DataFrame.info()</code></a> on a <code>tkinter</code> text widget so I need a string. However <code>pandas.DataFrame.info()</code> returns <code>NoneType</code> is there anyway I can change this?</p>

<pre><code>import pandas as pd
import numpy as np

data = np.random.rand(10).reshape(5,2)
cols = 'a', 'b'
df = pd.DataFrame(data, columns=cols)
df_info = df.info()
print(df_info)
type(df_info)
</code></pre>

<p>I'd like to do something like:</p>

<pre><code>info_str = ""
df_info = df.info(buf=info_str)
</code></pre>

<p>Is it possible to get <code>pandas</code> to return a string object from <code>DataFrame.info()</code>?</p>

<p>In the documentation you linked, there's a <code>buf</code> argument:</p>

<blockquote>
  <p>buf : writable buffer, defaults to sys.stdout</p>
</blockquote>

<p>So one option would be to pass a StringIO instance:</p>

<pre><code>&gt;&gt;&gt; import io
&gt;&gt;&gt; buf = io.StringIO()
&gt;&gt;&gt; df.info(buf=buf)
&gt;&gt;&gt; s = buf.getvalue()
&gt;&gt;&gt; type(s)
&lt;class 'str'&gt;
&gt;&gt;&gt; print(s)
&lt;class 'pandas.core.frame.DataFrame'&gt;
RangeIndex: 5 entries, 0 to 4
Data columns (total 2 columns):
a    5 non-null float64
b    5 non-null float64
dtypes: float64(2)
memory usage: 160.0 bytes
</code></pre>

python pandas