2016-09-11 5 views
3

pandas.DataFrame.info()の出力をtkinterテキストウィジェットに表示したいので、文字列が必要です。しかし、pandas.DataFrame.info()NoneTypeを返します。とにかくこれを変更できますか?私はのような何かやってみたいpandas.DataFrame.info()から文字列を返す方法

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) 

info_str = "" 
df_info = df.info(buf=info_str) 

pandasDataFrame.info()から文字列オブジェクトを返すために取得することが可能ですか?

答えて

6

リンクされたドキュメントでは、buf引数があります:

BUF:書き込み可能なバッファ、だから、1つのオプションのStringIOインスタンスを渡すことであろう

をsys.stdoutにデフォルト:

>>> import io 
>>> buf = io.StringIO() 
>>> df.info(buf=buf) 
>>> s = buf.getvalue() 
>>> type(s) 
<class 'str'> 
>>> print(s) 
<class 'pandas.core.frame.DataFrame'> 
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 
+0

ありがとうございます!私は 'buf'引数を見て、書き込むために空の文字列を与えようとしましたが、うまくいきませんでした。 bufに渡すことができる有効なオブジェクトのリストをどこに見つけることができるか知っていますか? – Bprodz

関連する問題