Pandas: Read CSV: ValueError: could not convert string to float

送分小仙女□ 提交于 2021-02-08 09:18:19

问题


I'm trying to read a large and complex CSV file with pandas.read_csv. The exact command is

pd.read_csv(filename, quotechar='"', low_memory=True, dtype=data_types, usecols= columns, true_values=['T'], false_values=['F'])

I am pretty sure that the data types are correct. I can read the first 16 million lines (setting nrows=16000000) without problems but somewhere after this I get the following error

ValueError: could not convert string to float: '1,123'

As it seems, for some reason pandas thinks two columns would be one.

What could be the problem? How can I fix it?


回答1:


I found the mistake. The problem was a thousand separator.

When writing the CSV file, most numbers were below thousand and were correctly written to the CSV file. However, this one value was greater than thousand and it was written as "1,123" which pandas did not recognize as a number but as a string.



来源:https://stackoverflow.com/questions/34319011/pandas-read-csv-valueerror-could-not-convert-string-to-float

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!