熊猫系列总和显示额外精度

2024-09-27 21:27:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图总结一个简单的熊猫系列,我得到了无关的结果(通过额外的精确性)。在

以下是场景:

import pandas as pd
prices = [2.99, 4.45, 1.36] 
s = pd.Series(prices)
s.sum()

显示输出:

^{pr2}$

我试过了:

pd.set_option('display.precision',2)  # No use - still the same result

除此之外:

np.set_printoptions(precision=2)  # no use

pd.show_versions()

该输出给出:

INSTALLED VERSIONS
------------------
commit: None
python: 3.6.6.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 61 Stepping 4, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.23.1
pytest: 3.0.5
pip: 9.0.1
setuptools: 39.1.0
Cython: 0.25.2
numpy: 1.13.3
scipy: 0.19.1
pyarrow: None
xarray: None
IPython: 5.1.0
sphinx: 1.5.1
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2016.10
blosc: None
bottleneck: 1.2.1
tables: 3.4.3
numexpr: 2.6.2
feather: None
matplotlib: 2.2.2
openpyxl: 2.4.1
xlrd: 1.0.0
xlwt: 1.2.0
xlsxwriter: 0.9.6
lxml: 3.7.2
bs4: 4.5.3
html5lib: 0.9999999
sqlalchemy: 1.1.5
pymysql: None
psycopg2: None
jinja2: 2.9.4
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: 0.6.0

有谁能帮我理解结果是怎么得到8.80000000000007的,以及如何只显示两个十进制数字。在

我也试过了-没用:

round(s.sum(),2)

Tags: importnonepandasosuseas场景precision
1条回答
网友
1楼 · 发布于 2024-09-27 21:27:21

如果您可以使用DataFrame,那么可以通过-

import pandas as pd
prices = [2.99, 4.45, 1.36] 
s = pd.DataFrame(prices)
with pd.option_context('display.precision', 2):
    print(s.sum())

将输出-

^{pr2}$

编辑: 或者,如果您坚持使用Series,那么您最终可以使用numpy函数around,如下所示-

>>> import numpy as np
>>> np.around(s.sum(), 2)
0    8.81

相关问题 更多 >

    热门问题