pandas :读取CSV:ValueError:无法将字符串转换为浮点型 [英] Pandas: Read CSV: ValueError: could not convert string to float

查看:83
本文介绍了 pandas :读取CSV:ValueError:无法将字符串转换为浮点型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用pandas.read_csv读取大型而复杂的CSV文件.确切的命令是

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

pd.read_csv(文件名,quotechar ='',low_memory = True,dtype = data_types,usecols =列,true_values = ['T'],false_values = ['F'])

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

我非常确定数据类型正确.我可以毫无问题地读取前1600万行(设置nrows = 16000000),但是在此之后出现以下错误

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:无法将字符串转换为float:'1,123'

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

看起来,出于某种原因,pandas认为两列将是一列.

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

可能是什么问题?我该如何解决?

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

推荐答案

我发现了错误.问题是千位分隔符.

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

写入CSV文件时,大多数数字都在1000以下,并且已正确写入CSV文件.但是,这个值大于千,它被写为"1,123",熊猫不能识别为数字,而是一个字符串.

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.

这篇关于 pandas :读取CSV:ValueError:无法将字符串转换为浮点型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆