suppress Name dtype from python pandas describe

匿名 (未验证) 提交于 2019-12-03 08:42:37

问题:

Lets say I have

r = pd.DataFrame({'A':1 ,               'B':pd.Series(1,index=list(range(4)),dtype='float32')}) 

And r['B'].describe()[['mean','std','min','max']] gives an output :

mean    1.0 std     0.0 min     1.0 max     1.0 Name: B, dtype: float64 

But from the above output , how should I get rid or suppress the last line " Name:B, dtype: float64 "

I figured out one way to achieve this

x=r['B'].describe()[['mean','std','min','max']] print "mean ",x['mean'],"\nstd ",x['std'],"\nmin ",x['min'],"\nmax ",x['max']   

which gives the desired output :

mean  1.0  std  0.0  min  1.0  max  1.0  

Is there any cleaner to achieve this output directly from pd.describe( )

回答1:

If need output as DataFrame add reset_index:

x=r['B'].describe()[['mean','std','min','max']].reset_index() print (x)   index    B 0  mean  1.0 1   std  0.0 2   min  1.0 3   max  1.0 

And then use DataFrame.to_string:

print (x.to_string(header=None, index=None)) mean  1.0  std  0.0  min  1.0  max  1.0 


回答2:

better answer
use to_csv on dataframe

rd = r.B.describe()[['mean','std','min','max']].reset_index() print(rd.to_csv(header=None, index=None, sep='\t'))  mean    1.0 std     0.0 min     1.0 max     1.0 

old answer

for name, value in r['B'].describe()[['mean','std','min','max']].iteritems():     print('{:<5s} {:2.1f}'.format(name, value))  mean  1.0 std   0.0 min   1.0 max   1.0 


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