【问题标题】:what is the easiest way to subtract 2 columns of float in dataframe?在数据框中减去 2 列浮点数的最简单方法是什么?
【发布时间】:2022-01-05 04:31:09
【问题描述】:

假设我有一个包含 2 列 floats 和 4 或 5 位 pricision 数字的数据框,如下所示:

dt = pd.DataFrame({"OPEN": [-0.00011,-0.0114, 0.0066,-0.0044,-0.0012,-0.0005,
                             0.0005,-0.0037, -0.0029, 0.0034, 0.0003, 0.0001 ],
                   "CLOSE": [-1.20011,-3.0114, 0.4066,-0.0074,-0.0016,-5.0005,
                             0.0225,-0.0027, -0.0026, 0.0064, 0.0043, 4.0001 ],})

我想正确获取dt["delta"]=dt["OPEN"] - dt["CLOSE"]。但是,由于这些列是floats,因此结果值不准确!所以,例如0.0003-0.0002 应该是0.0001 但是它给了我0.0000999999999999999

这是我当前的解决方案,但它不起作用!

from decimal import *
getcontext().prec = 6

delta = []
for i in np.arange((dt.size)-1):

    print("CLOSE: ",Decimal(dt.loc[i,"Close"]))
    delta.append(Decimal(dt.loc[i,"Close"]) - Decimal(dt.loc[i,"Open"]))

print("delta",delta)

我的问题是,如何正确减去“CLOSE”和“OPEN”列?

要点:

当我运行我的代码时,我得到以下打印。我想知道,为什么十进制值仍然有 52 位,同时我指定了 prec==6

`CLOSE:  1.0578000000000000735411731511703692376613616943359375

CLOSE:  1.046300000000000007815970093361102044582366943359375

CLOSE:  1.052799999999999958077978590154089033603668212890625

CLOSE:  1.0484999999999999875655021241982467472553253173828125

CLOSE:  1.0471999999999999086952584548271261155605316162109375

CLOSE:  1.0464999999999999857891452847979962825775146484375

CLOSE:  1.047099999999999919708670859108678996562957763671875`

【问题讨论】:

  • 你的意思是什么?浮点数永远不会精确。你是什​​么意思你的解决方案不起作用?
  • 所以,例如0.0003-0.0002 应该是0.0001 但是它给了我0.0000999999999999999

标签: python precision floating-accuracy


【解决方案1】:

在使用 decimal 模块时设置 getcontext().prec = 6 应该可以工作:

In [1]: import pandas as pd
   ...: from decimal import Decimal, getcontext
In [2]: getcontext().prec = 6
In [3]: df = pd.DataFrame({
   ...:     "OPEN": [
   ...:         -0.00011, -0.0114, 0.0066, -0.0044, -0.0012, -0.0005, 0.0005, -0.0037,
   ...:         -0.0029, 0.0034, 0.0003, 0.0001
   ...:     ],
   ...:     "CLOSE": [
   ...:         -1.20011, -3.0114, 0.4066, -0.0074, -0.0016, -5.0005, 0.0225, -0.0027,
   ...:         -0.0026, 0.0064, 0.0043, 4.0001
   ...:     ],
   ...: })
In [4]: df
Out[4]: 
       OPEN    CLOSE
0  -0.00011 -1.20011
1  -0.01140 -3.01140
2   0.00660  0.40660
3  -0.00440 -0.00740
4  -0.00120 -0.00160
5  -0.00050 -5.00050
6   0.00050  0.02250
7  -0.00370 -0.00270
8  -0.00290 -0.00260
9   0.00340  0.00640
10  0.00030  0.00430
11  0.00010  4.00010
In [5]: df["DELTA"] = df["OPEN"].apply(Decimal) - df["CLOSE"].apply(Decimal)
In [6]: df
Out[6]: 
       OPEN    CLOSE         DELTA
0  -0.00011 -1.20011       1.20000
1  -0.01140 -3.01140       3.00000
2   0.00660  0.40660     -0.400000
3  -0.00440 -0.00740    0.00300000
4  -0.00120 -0.00160   0.000400000
5  -0.00050 -5.00050       5.00000
6   0.00050  0.02250    -0.0220000
7  -0.00370 -0.00270   -0.00100000
8  -0.00290 -0.00260  -0.000300000
9   0.00340  0.00640   -0.00300000
10  0.00030  0.00430   -0.00400000
11  0.00010  4.00010      -4.00000

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 2012-12-06
    • 2015-01-11
    相关资源
    最近更新 更多