抑制来自 python pandas 的 Name dtype 描述

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40278845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:17:44  来源:igfitidea点击:

suppress Name dtype from python pandas describe

pythonpandasdataframeformatseries

提问by sus

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 :

r['B'].describe()[['mean','std','min','max']]给出一个输出:

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"

但是从上面的输出,我应该如何摆脱或抑制最后一行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( )

是否有任何清洁器可以直接从 pd.describe() 实现此输出

回答by jezrael

If need output as DataFrameadd reset_index:

如果需要输出为DataFrameadd 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:

然后使用DataFrame.to_string

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

回答by piRSquared

better answer
use to_csvon dataframe

在数据框上
使用更好的答案to_csv

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