python - python错误:ValueError:float()的无效文字:0,69
问题描述
“我必须将对象列表 ['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
解决方案
在应用浮点数之前,您必须用点替换逗号,否则浮点函数会认为您正在给参数
推荐阅读
- python - pyinstaller 的 Python 路径配置
- c# - 如何抛出和捕获异常
- servicestack - 403 Forbidden Response期间如何防止ServiceStack泄露私服信息
- node.js - Appium 没有安装
- powerbi - 如果值为零,是否可以隐藏 PowerBI 饼图中的图例?
- excel - 以厘米为单位的形状尺寸不一致
- excel - Excel VBA XML HTTP 请求在登录页面上抓取表格
- angular - Angular ag-grid:如何在单元格中呈现 HTML?
- c# - Azure AD 组和用户分配的托管标识
- reactjs - 多次反应渲染