简体   繁体   English

Pandas read_csv 在末尾错误地添加了零和一个随机数

[英]Pandas read_csv wrongly adds zeros and a random number at the end

I have a weird bug (?) when reading a csv with read_csv function.使用 read_csv 函数读取 csv 时,我有一个奇怪的错误 (?)。 Some of the numbers (in my concrete case in 11 lines from a total of 500) are read with many trailing zeros and a seemingly random number at the end.一些数字(在我的具体案例中,总共 500 行中有 11 行)在读取时带有许多尾随零和一个看似随机的数字。

For example, for a value that is "0.052" in the csv, when I run pandas I get this:例如,对于 csv 中的“0.052”值,当我运行 Pandas 时,我得到以下信息:

values = pd.read_csv(filename, header=2)
values.column1[487]
0.052000000000000005

This is happening just for some columns, others are read normally.这仅发生在某些列上,其他列正常读取。

Any ideas of what is going on here?关于这里发生了什么的任何想法?

It probably is the data type.它可能是数据类型。 Specifying the datatype will solve it.指定数据类型将解决它。 If you just want to change the representation use:如果您只想更改表示,请使用:

pd.set_option("display.precision", *number of numbers behind the comma*)
pd.set_option("display.precision", 3)

If you would want to visualize it to 0.052.如果您想将其可视化为 0.052。 Put this.把这个。 pd.set_option before the output (preferably at the top). pd.set_option 在输出之前(最好在顶部)。 NOTE: This only shows 0.052 but pandas still calculates with 0.052000005 which in most cases isn't relevant.注意:这仅显示 0.052,但 Pandas 仍然使用 0.052000005 进行计算,这在大多数情况下是不相关的。 But in your case it might.但在你的情况下它可能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM