任何人都可以帮忙告诉我为什么 pd.to_numeric 会抛出 valueerror 吗?

发布于 2025-01-09 22:28:19 字数 1639 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扬花落满肩 2025-01-16 22:28:19

您的 StackTrace 结尾为:ValueError: 无法解析位置 15264 处的字符串“-”

因此,Ave​​rage_price 列可能只包含“-”,意思是
实际上“没有数据”。

指示的位置(15264)可能是行号,表示第一个
转换失败的地方。

可能的解决方案之一是将此类不可转换的情况转换为 NaN

clean_properties['Average_price'] = pd.to_numeric(
    clean_properties['Average_price'], errors='coerce')

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:

clean_properties['Average_price'] = pd.to_numeric(
    clean_properties['Average_price'], errors='coerce')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文