Suppress descriptive output when printing pandas dataframe

橙三吉。 提交于 2019-11-30 17:38:57

问题


Say I have dataframe, c:

a=np.random.random((6,2))
c=pd.DataFrame(a)
c.columns=['A','B']

printing row 0 values:

print c.loc[(0),:]

results in:

A    0.220170
B    0.261467
Name: 0, dtype: float64

I would like to suppress the Name: 0, dtype: float64 line so that I just get:

A    0.220170
B    0.261467

Does anyone know how?

(n.b. I am appending this to a text file)


回答1:


You can tweak the __unicode__ method for a Series:

In [11]: s = pd.Series([1, 2])

In [12]: s
Out[12]:
0    1
1    2
dtype: int64

In [13]: pd.Series.__unicode__ = pd.Series.to_string

In [14]: s  # same with print
Out[14]:
0    1
1    2

To append to a csv use append mode (see this or this question).



来源:https://stackoverflow.com/questions/24295451/suppress-descriptive-output-when-printing-pandas-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!