gpt4 book ai didi

python - 在 Pandas 中使用 read_csv 时精度丢失

转载 作者:太空狗 更新时间:2023-10-29 18:20:56 27 4
gpt4 key购买 nike

我在一个文本文件中有以下格式的文件,我正在尝试将其读入 pandas 数据框。

895|2015-4-23|19|10000|LA|0.4677978806|0.4773469340|0.4089938425|0.8224291972|0.8652525793|0.6829942860|0.5139162227|

如您所见,输入文件中的 float 后有 10 个整数。

df = pd.read_csv('mockup.txt',header=None,delimiter='|')

当我尝试将它读入数据框时,我没有得到最后 4 个整数

df[5].head()

0 0.467798
1 0.258165
2 0.860384
3 0.803388
4 0.249820
Name: 5, dtype: float64

如何获得输入文件中存在的完整精度?我有一些矩阵运算需要执行,所以我不能将它转换为字符串。

我发现我必须对 dtype 做些什么,但我不确定应该在哪里使用它。

最佳答案

只是显示问题,见docs :

#temporaly set display precision
with pd.option_context('display.precision', 10):
print df

0 1 2 3 4 5 6 7 \
0 895 2015-4-23 19 10000 LA 0.4677978806 0.477346934 0.4089938425

8 9 10 11 12
0 0.8224291972 0.8652525793 0.682994286 0.5139162227 NaN

编辑:(谢谢 Mark Dickinson):

Pandas uses a dedicated decimal-to-binary converter that sacrifices perfect accuracy for the sake of speed. Passing float_precision='round_trip' to read_csv fixes this. See the documentation for more.

关于python - 在 Pandas 中使用 read_csv 时精度丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36909368/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com