Python Pandas 数据精度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43217916/
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-08-19 22:39:26  来源:igfitidea点击:

Pandas data precision

pythonpandas

提问by user7789097

By default the numerical values in data frame are stored up to 6 decimals only. How do I get the full precision.

默认情况下,数据框中的数值最多只能存储 6 位小数。我如何获得完整的精度。

For example
34.98774564765 is stored as 34.987746. I do want the full value.

例如
34.98774564765 存储为 34.987746。我确实想要完整的价值。

and 0.00000565 is stored as 0. .

并且 0.00000565 存储为 0. .

Apart from applying formats to each data frame is there any global setting that helps preserving the precision.

除了将格式应用于每个数据框之外,还有任何有助于保持精度的全局设置。

Thanks

谢谢

回答by donkopotamus

No, 34.98774564765is merely being printed by default with six decimal places:

不,34.98774564765只是默认打印六位小数:

>>> pandas.DataFrame([34.98774564765])
           0
0  34.987746

The data itself has more precision:

数据本身具有更高的精度:

>>> pandas.DataFrame([34.98774564765])[0].data[0]
34.98774564765

You can change the default used for printing frames by altering pandas.options.display.precision.

您可以通过更改pandas.options.display.precision.

For example:

例如:

>>> pandas.set_option("display.precision", 8)
>>> pandas.DataFrame([34.98774564765])
                0
0  34.98774564765

回答by piRSquared

You can also use the 'display.float_format'option

您也可以使用该'display.float_format'选项

with pd.option_context('display.float_format', '{:0.20f}'.format):
    print(pd.DataFrame([34.98774564765]))

                        0
0 34.98774564765000150146

回答by MaxU

Your data is storedwith the precision, corresponding to your dtype (np.float16, np.float32, np.float64).

您的数据以对应于您的 dtype ( , , )的精度存储np.float16np.float32np.float64

pd.options.display.precision- allows you to change the precision for printingthe data

pd.options.display.precision- 允许您更改printing数据的精度