将对象转换为float会丢失太多精度 - 熊猫

tho*_*hor 4 python floating-point-precision pandas

我正在尝试绘制一个DataFrame使用pandas但它不起作用(有关详细信息,请参阅此类似的线程).我认为问题的一部分可能是我的DataFrame似乎是objects:

>>> df.dtypes
Field          object
Moment         object
Temperature    object
Run Code Online (Sandbox Code Playgroud)

但是,如果我要将所有值转换为类型float,我会失去很多精度.列Moment中的所有值都是表单-132.2036E-06并转换为将floatdf1 = df.astype(float)更改为-0.000132.

有谁知道如何保持精度?

Jef*_*eff 6

您可以这样做以更改显示的精度

In [1]: df = DataFrame(np.random.randn(5,2))

In [2]: df
Out[2]: 
          0         1
0  0.943371  0.171686
1  1.508525  0.005589
2 -0.764565  0.259490
3 -1.059662 -0.837602
4  0.561804 -0.592487

[5 rows x 2 columns]

In [3]: pd.set_option('display.precision',12)

In [4]: df
Out[4]: 
               0              1
0  0.94337126946  0.17168604324
1  1.50852519105  0.00558907755
2 -0.76456509501  0.25948965731
3 -1.05966206139 -0.83760201886
4  0.56180449801 -0.59248656304

[5 rows x 2 columns]
Run Code Online (Sandbox Code Playgroud)