python error: ValueError: invalid literal for float(): 0,69
python error: ValueError: invalid literal for float(): 0,69
“我必须将对象列表的格式 ['Dev'] 更改为 float64,但是 python 向我发送此错误。'Dev'它是来自 csv 的一列对象table ('prueba.csv') 我需要翻译它以创建 statistics/metrics,你能帮我吗?"
#开始代码
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
你必须在应用 float 之前用点替换逗号,否则 float 函数会认为你在给参数赋值
看起来你没有替换逗号
def convert_currency(val):
"""
- Remove commas and u'
- Convert to float type
"""
new_val = val.replace("u'","").replace(",",".")
return float(new_val)
会成功的。
你的花车需要点而不是你说的逗号。
只需将编码更改为 latin_1
。
我在本地试过,效果很好,它自动将其 dtype 分配为 float64。
“我必须将对象列表的格式 ['Dev'] 更改为 float64,但是 python 向我发送此错误。'Dev'它是来自 csv 的一列对象table ('prueba.csv') 我需要翻译它以创建 statistics/metrics,你能帮我吗?"
#开始代码
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
你必须在应用 float 之前用点替换逗号,否则 float 函数会认为你在给参数赋值
看起来你没有替换逗号
def convert_currency(val):
"""
- Remove commas and u'
- Convert to float type
"""
new_val = val.replace("u'","").replace(",",".")
return float(new_val)
会成功的。
你的花车需要点而不是你说的逗号。
只需将编码更改为 latin_1
。
我在本地试过,效果很好,它自动将其 dtype 分配为 float64。