pandas 打印选项 [英] Pandas printing options

查看:75
本文介绍了 pandas 打印选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在控制台中打印不同的数字格式信息(不是e"形式.示例:现在打印为 -1.0e-02,我想看到 1.000,点后有 3 位数字).

I would like to print different number format information (not the 'e' form. Example: Now is printed as -1.0e-02, I would like to see 1.000, with 3 digits after dot) in my console.

  • 熊猫 0.18.1
  • Python 2.7

Python 代码

from pandas import set_option
set_option('display.width', 100)
set_option('precision', 3)
correlations = transformed_data['train'].corr(method='pearson')
print(correlations)

输出

    col0       col2   col4      col10  col11  col12      label      col14
col0   1.000e+00 -8.636e-05 -0.016  7.884e-02  0.049 -0.007  2.350e-01 -1.497e-02
col2  -8.636e-05  1.000e+00 -0.001 -1.936e-03 -0.003  0.019 -1.670e-03  1.068e-02
col4  -1.561e-02 -1.425e-03  1.000 -7.065e-03 -0.006  0.006  4.412e-03 -6.901e-03
col10  7.884e-02 -1.936e-03 -0.007  1.000e+00 -0.032  0.001  2.271e-01 -2.332e-04
col11  4.870e-02 -2.979e-03 -0.006 -3.157e-02  1.000 -0.003  1.416e-01 -5.507e-03
col12 -6.520e-03  1.852e-02  0.006  1.114e-03 -0.003  1.000  3.778e-03 -9.215e-03
label  2.350e-01 -1.670e-03  0.004  2.271e-01  0.142  0.004  1.000e+00  7.549e-04
col14 -1.497e-02  1.068e-02 -0.007 -2.332e-04 -0.006 -0.009  7.549e-04  1.000e+00

数据类型

print(transformed_data['train'].dtypes)

    col0     float64
    col1      object
    col2     float64
    col3      object
    col4     float64
    col5      object
    col6      object
    col7      object
    col8      object
    col9      object
    col10    float64
    col11    float64
    col12    float64
    col13     object
    label      int64
    col14    float64

推荐答案

既然你提到了 set_option('precision', 3),DataFrame 在 value 中显示任何 valuecode>1.000(示例)格式就像您需要的一样,
除非 value <1e-3(或 0.001).

Since you mentioned set_option('precision', 3), the DataFrame displays any value in 1.000 (example) format just like you need,
EXCEPT when value < 1e-3 (or 0.001).

在这种情况下,该值不能被截断为 0.000.因此,保留了科学记数法.
但是,Pandas DataFrame 为每一列维护一个恒定的显示格式.因此,科学记数法保留在包含 value < 的整个列中.1e-3

In such a situation, the value cannot be truncated to 0.000. Thus, Scientific notation is preserved.
But, A Pandas DataFrame maintains one constant display format for each column. So, the Scientific notation is preserved in the entire column that contains value < 1e-3

要确保所有值都以非科学格式显示,您可以使用:

To make sure all values are displayed in non-scientific format, you can use:

set_option('precision', 6)

或者您可以在将所有值存储到 DataFrame 之前对其进行截断或舍入

OR you can truncate or round all the values before storing it in the DataFrame

这篇关于 pandas 打印选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆