任何人都可以帮忙告诉我为什么 pd.to_numeric 会抛出 valueerror 吗?
clean_properties 是数据集,Average_price 是其中一列。它当前的数据类型是对象,我需要将其转换为浮点数。我在网上查看的所有内容都表明这是正确的格式,但它会引发 ValueError。
这是数据帧的 .head()
London_Borough ID Month Average_price
0 City of London E09000001 1995-01-01 91448.98487
1 Barking & Dagenham E09000002 1995-01-01 50460.2266
2 Barnet E09000003 1995-01-01 93284.51832
3 Bexley E09000004 1995-01-01 64958.09036
4 Brent E09000005 1995-01-01 71306.56698
这就是我正在尝试抛出错误的方法
clean_properties['Average_price'] = pd.to_numeric(clean_properties['Average_price'])
ValueError Traceback (most recent call last)
~\anaconda33\lib\site-packages\pandas\_libs\lib.pyx in pandas._libs.lib.maybe_convert_numeric()
ValueError: Unable to parse string "-"
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_30444/2671958707.py in <module>
1 # Try this here
----> 2 clean_properties['Average_price'] = pd.to_numeric(clean_properties['Average_price'])
~\anaconda33\lib\site-packages\pandas\core\tools\numeric.py in to_numeric(arg, errors, downcast)
181 coerce_numeric = errors not in ("ignore", "raise")
182 try:
--> 183 values, _ = lib.maybe_convert_numeric(
184 values, set(), coerce_numeric=coerce_numeric
185 )
~\anaconda33\lib\site-packages\pandas\_libs\lib.pyx in pandas._libs.lib.maybe_convert_numeric()
ValueError: Unable to parse string "-" at position 15264
clean_properties is the dataset, and Average_price is one of the columns. Its current dtype is object, and I need to convert it to a float. Everything I've looked at online says this is the correct format, but it throws a ValueError.
here is the .head() for the dataframe
London_Borough ID Month Average_price
0 City of London E09000001 1995-01-01 91448.98487
1 Barking & Dagenham E09000002 1995-01-01 50460.2266
2 Barnet E09000003 1995-01-01 93284.51832
3 Bexley E09000004 1995-01-01 64958.09036
4 Brent E09000005 1995-01-01 71306.56698
And this is what I'm trying that is throwing an error
clean_properties['Average_price'] = pd.to_numeric(clean_properties['Average_price'])
ValueError Traceback (most recent call last)
~\anaconda33\lib\site-packages\pandas\_libs\lib.pyx in pandas._libs.lib.maybe_convert_numeric()
ValueError: Unable to parse string "-"
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_30444/2671958707.py in <module>
1 # Try this here
----> 2 clean_properties['Average_price'] = pd.to_numeric(clean_properties['Average_price'])
~\anaconda33\lib\site-packages\pandas\core\tools\numeric.py in to_numeric(arg, errors, downcast)
181 coerce_numeric = errors not in ("ignore", "raise")
182 try:
--> 183 values, _ = lib.maybe_convert_numeric(
184 values, set(), coerce_numeric=coerce_numeric
185 )
~\anaconda33\lib\site-packages\pandas\_libs\lib.pyx in pandas._libs.lib.maybe_convert_numeric()
ValueError: Unable to parse string "-" at position 15264
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 StackTrace 结尾为:ValueError: 无法解析位置 15264 处的字符串“-”。
因此,Average_price 列可能只包含“-”,意思是
实际上“没有数据”。
指示的位置(15264)可能是行号,表示第一个
转换失败的地方。
可能的解决方案之一是将此类不可转换的情况转换为 NaN:
Your StackTrace ends with: ValueError: Unable to parse string "-" at position 15264.
So probably Average_price column contains somewhere only "-", meaning
actually "no data".
The indicated position (15264) is probably the row number, indicating the first
place where the conversion failed.
One of possible solutions is to convert such inconvertible cases to NaN: