【问题标题】:python error: ValueError: invalid literal for float(): 0,69python错误:ValueError:float()的无效文字:0,69
【发布时间】:2020-11-04 10:17:16
【问题描述】:

“我必须将对象列表 ['Dev'] 的格式更改为 float64,但 python 向我发送此错误。'Dev'是 csv 表 ('prueba.csv') 中的一列对象,我需要翻译它以创建统计数据/指标,你能帮我吗?”

#开始代码

import pandas as pd
import numpy as np

#从.csv文件中读取数据;

df = pd.read_csv('prueba.csv', sep=';', decimal=',',encoding='ISO-8859-1')

#数据说明

df.dtypes

Element      object
Property     object
Nominal     float64
Actual       object
Tol -       float64
Tol +       float64
Dev          object
Check       float64
Out         float64
dtype: object

#用于浮动翻译的列表;

df['Dev']
0        0,69
1        0,62
2        0,54
3        0,47
4        0,19
5       -0,26
6        0,11
7         0,1
8         0,2
9        0,29
10      -1,54
11         -2
12      -2,06
13      -2,02
14      -2,08
15      -1,39
16      -1,68
17      -1,91
18      -1,78
19       -1,8
20      -1,21
21      -1,07
22      -0,97
23      -1,47
24      -1,35
25      -0,91
26      -1,17
27      -0,67
28      -1,12
29      -1,13
1962        0
1963    -0,37
1964     0,02
1965     0,32
1966     0,04
1967        0
1968     0,39
1969     0,25
1970     0,38
1971     0,15
1972        0
1973     1,11
1974    -1,13
1975     0,15
1976     0,12
1977        0
1978    -0,47
1979    -0,85
1980     0,08
1981     0,23
1982        0
1983     1,03
1984    -0,76
1985    -0,03
1986     0,02
1987        0
1988     0,36
1989    -1,45
1990     0,12
1991     0,09
Name: Dev, Length: 1992, dtype: object

#转换的函数定义

def convert_currency(val):
    """
     - Remove commas and u'
     - Convert to float type
    """
    new_val = val.replace("u'","'")
    return float(new_val)

df['Dev'].apply(convert_currency)

#Python 错误;

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-93-9ac4450806f7> in <module>()
----> 1 df['Dev'].apply(convert_currency)

C:\Users\kaosb\miniconda2\lib\site-packages\pandas\core\series.pyc in apply(self, func, convert_dtype, args, **kwds)
   3589             else:
   3590                 values = self.astype(object).values
-> 3591                 mapped = lib.map_infer(values, f, convert=convert_dtype)
   3592 
   3593         if len(mapped) and isinstance(mapped[0], Series):

pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()

<ipython-input-89-0c43557e37eb> in convert_currency(val)
      5     """
      6     new_val = val.replace("u'","'")
----> 7     return float(new_val)

ValueError: invalid literal for float(): 0,69

【问题讨论】:

  • 熊猫可能无法识别编码是“西欧”格式。你能用encoding=latin_1再试一次吗(它只是西欧编码的另一个别名)

标签: python csv floating-point jupyter


【解决方案1】:

看起来你没有替换逗号

def convert_currency(val):
    """
     - Remove commas and u'
     - Convert to float type
    """
    new_val = val.replace("u'","").replace(",",".")
    return float(new_val)

会成功的。

你的花车需要点而不是你说的逗号。

【讨论】:

  • 我试过但是...; ValueError:无法将字符串转换为浮点数:???
  • 这意味着是时候调试了。使用 try 和 except 对 float(new_val) 函数进行子句,如果它抛出错误,则打印它并给出默认值。这样你就可以看到为什么那不起作用。我的猜测是你有一些奇怪的字符导致功能失败
  • 谢谢哈盖卡林霍夫
  • 没问题的朋友!请将此答案标记为“已接受”,以便其他人也可以查看。
【解决方案2】:

在应用浮点数之前,您必须用点替换逗号,否则浮点函数会认为您正在给参数提供参数

【讨论】:

    【解决方案3】:

    只需将您的编码更改为latin_1。 我在本地尝试过,效果很好,它会自动将其 dtype 分配为 float64。

    【讨论】:

    • 如何更改编码?也许像这样? df = pd.read_csv('prueba.csv', sep=';', decimal=',',encoding='latin_1 ') 谢谢!!
    猜你喜欢
    • 2013-01-15
    • 1970-01-01
    • 2018-02-19
    • 2018-06-08
    • 1970-01-01
    • 2017-10-13
    • 2016-10-05
    • 2023-03-31
    • 2016-05-31
    相关资源
    最近更新 更多