简体   繁体   English

如何修复“ValueError:无法将字符串转换为浮点数”

[英]How to fix “ValueError: could not convert string to float”

I am going to train an SVM based on x dataframe and y series I have.我将基于我拥有的 x dataframe 和 y 系列训练一个 SVM。 X dataframe is shown below: X dataframe 如下图所示:

x: X:

Timestamp      Location of sensors       Pressure or Flow values

0.00000         138.22, 1549.64            28.92

0.08333         138.22, 1549.64            28.94

0.16667         138.22, 1549.64            28.96

In X dataframe, location of sensors are represented as the form of node coordinate.在 X dataframe 中,传感器的位置以节点坐标的形式表示。 Y series is shown below: Y系列如下图:

y:是:

0
0
0

But when I fit svm to the training set, it returned an ValueError: could not convert string to float: '361.51,1100.77' and (361.51, 1100.77) are coordinates for a node.但是当我将 svm 拟合到训练集时,它返回了一个 ValueError: could not convert string to float: '361.51,1100.77'和 (361.51, 1100.77) 是节点的坐标。 Could you please give me some ideas to fix this problem?你能给我一些想法来解决这个问题吗? Appreciate a lot if any advices.如果有任何建议,非常感谢。

I'm assuming that you are trying to convert the entire string "361.51,1100.77" into a float, and you can see why that's a problem because Python sees two decimal points and a comma inbetween, so it has no idea what to do.我假设您正在尝试将整个字符串“361.51,1100.77”转换为浮点数,您可以看到为什么这是一个问题,因为 Python 看到两个小数点和一个逗号,所以它不知道该怎么做。 Assuming you want the numbers to be separate, you could do something like this:假设您希望数字分开,您可以执行以下操作:

myStr = "361.51,1100.77"
x = float(myStr[0:myStr.index(",")])
y = float(myStr[myStr.index(",")+1:])
print(x)
print(y)

Which would get you an output of这会给你一个 output

361.51
1100.77

Assigning x to be myStr[0:myStr.index(",")] takes a substring of the original string from 1 to the first occurrence of a comma, getting you the first number.将 x 分配为myStr[0:myStr.index(",")]将原始字符串的 substring 从 1 到第一次出现逗号,得到第一个数字。

Assigning y to be myStr[myStr.index(",")+1:] takes a substring of the original string starting after the first comma and to the end of the string, getting you the second number.将 y 指定为myStr[myStr.index(",")+1:]需要原始字符串的 substring 从第一个逗号开始到字符串末尾,得到第二个数字。

Both can easily be converted to floats from here using the float(myStr) method, getting you two separate floats.两者都可以使用float(myStr)方法轻松地从此处转换为浮点数,从而为您提供两个单独的浮点数。

Here is a helpful link to understand string slicing: https://www.geeksforgeeks.org/string-slicing-in-python/这是了解字符串切片的有用链接: https://www.geeksforgeeks.org/string-slicing-in-python/

'361.51,1100.77' are actually two numbers right? '361.51,1100.77' 实际上是两个数字,对吗? A latitude (361.51) and a longitude (1100.77).一个纬度 (361.51) 和一个经度 (1100.77)。 You would first need to split it into two strings.您首先需要将其拆分为两个字符串。 Here is one way to do it:这是一种方法:

data = pd.DataFrame(data=[[0, "138.22,1549.64", 28.92]], columns=["Timestamp", "coordinate", "flow"])

data["latitude"] = data["coordinate"].apply(lambda x: float(x.split(",")[0]))
data["longitude"] = data["coordinate"].apply(lambda x: float(x.split(",")[1]))

This will give you two new columns in your dataframe each with a float of the values in the string.这将在 dataframe 中为您提供两个新列,每个列都有字符串中的浮点值。

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

相关问题 如何修复“ValueError:无法将字符串转换为浮点数”? - How to fix “ValueError: could not convert string to float”? 如何修复 ValueError :无法将字符串转换为浮点数:在 Python 中 - how to fix ValueError :could not convert string to float: in Python 如何修复“ValueError:无法将字符串转换为浮点数:'East'”(Python) - How to fix "ValueError: could not convert string to float: 'East'" (Python) 如何使用tkinter修复Python中的“ ValueError:无法将字符串转换为float:” - How to fix “ValueError: could not convert string to float:” in Python with tkinter 如何修复此错误:ValueError:无法将字符串转换为浮点数:'A' - How to fix this error: ValueError: could not convert string to float: 'A' 如何解决此ValueError:无法将字符串转换为float? - How to workaround this ValueError: could not convert string to float? ValueError:无法将字符串转换为float:' ' - ValueError: could not convert string to float: '���' ValueError: 无法将字符串转换为浮点数:'-' - ValueError: could not convert string to float: '-' ValueError:无法将字符串转换为浮点数:''" - ValueError: could not convert string to float: '' " ValueError:无法将字符串转换为浮点数:“否” - ValueError: could not convert string to float: 'no'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM