值错误:无法将字符串转换为浮点数:"571,2"



我写的代码是:df['Liquid Milk'] = df['Liquid Milk'].replace("", np.nan).astype('float64')

我在下面得到一个错误,不确定哪里是错误,已经尝试了许多不同的方法,但仍然是相同的错误。如有任何帮助,不胜感激。

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-52-607dcacd5a1a> in <module>
----> 1 m['Liquid Milk(Mil Litres)']=m['Liquid Milk(Mil Litres)'].replace("", np.nan).astype('float64')
2 
3 
4 
/usr/local/lib/python3.6/dist-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5679             # else, only a single dtype is given
5680             new_data = self._data.astype(dtype=dtype, copy=copy, errors=errors,
-> 5681                                          **kwargs)
5682             return self._constructor(new_data).__finalize__(self)
5683 
/usr/local/lib/python3.6/dist-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
529 
530     def astype(self, dtype, **kwargs):
--> 531         return self.apply('astype', dtype=dtype, **kwargs)
532 
533     def convert(self, **kwargs):
/usr/local/lib/python3.6/dist-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
393                                             copy=align_copy)
394 
--> 395             applied = getattr(b, f)(**kwargs)
396             result_blocks = _extend_blocks(applied, result_blocks)
397 
/usr/local/lib/python3.6/dist-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
532     def astype(self, dtype, copy=False, errors='raise', values=None, **kwargs):
533         return self._astype(dtype, copy=copy, errors=errors, values=values,
--> 534                             **kwargs)
535 
536     def _astype(self, dtype, copy=False, errors='raise', values=None,
/usr/local/lib/python3.6/dist-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
631 
632                     # _astype_nansafe works fine with 1-d only
--> 633                     values = astype_nansafe(values.ravel(), dtype, copy=True)
634 
635                 # TODO(extension)
/usr/local/lib/python3.6/dist-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
700     if copy or is_object_dtype(arr) or is_object_dtype(dtype):
701         # Explicit copy, or required since NumPy can't view from / to object.
--> 702         return arr.astype(dtype, copy=True)
703 
704     return arr.view(dtype)
ValueError: could not convert string to float: '571,2'

浮点数的整数部分和分数部分必须用.分隔,不能用,分隔。将所有,s替换为.s

float("571.2")

工作
float("571,2")

会失败。

最新更新