我正在使用Pandas和Jupyter Notebook使用Python中的数据框,我的数据框具有经度和纬度列,值为'-23,4588'。不知何故,每次我试图将其转换为float,我得到一个错误告诉'could not convert string to float'。
我试图改变逗号,试图改变。csv列类型为浮动,但没有工作。
我的一部分代码:
ValueError Traceback (most recent call last)
C:TEMP/ipykernel_12640/4061618161.py in <module>
----> 1 newocorr_sjc['Latitude'] = newocorr_sjc['Latitude'].astype(float)
c:userscaique.fernandesappdatalocalprogramspythonpython39libsite-packagespandascoregeneric.py in astype(self, dtype, copy, errors)
5875 else:
5876 # else, only a single dtype is given
-> 5877 new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors)
5878 return self._constructor(new_data).__finalize__(self, method="astype")
5879
c:userscaique.fernandesappdatalocalprogramspythonpython39libsite-packagespandascoreinternalsmanagers.py in astype(self, dtype, copy, errors)
629 self, dtype, copy: bool = False, errors: str = "raise"
630 ) -> "BlockManager":
--> 631 return self.apply("astype", dtype=dtype, copy=copy, errors=errors)
632
633 def convert(
c:userscaique.fernandesappdatalocalprogramspythonpython39libsite-packagespandascoreinternalsmanagers.py in apply(self, f, align_keys, ignore_failures, **kwargs)
425 applied = b.apply(f, **kwargs)
426 else:
--> 427 applied = getattr(b, f)(**kwargs)
428 except (TypeError, NotImplementedError):
429 if not ignore_failures:
c:userscaique.fernandesappdatalocalprogramspythonpython39libsite-packagespandascoreinternalsblocks.py in astype(self, dtype, copy, errors)
671 vals1d = values.ravel()
672 try:
--> 673 values = astype_nansafe(vals1d, dtype, copy=True)
674 except (ValueError, TypeError):
675 # e.g. astype_nansafe can fail on object-dtype of strings
c:userscaique.fernandesappdatalocalprogramspythonpython39libsite-packagespandascoredtypescast.py in astype_nansafe(arr, dtype, copy, skipna)
1095 if copy or is_object_dtype(arr) or is_object_dtype(dtype):
1096 # Explicit copy, or required since NumPy can't view from / to object.
-> 1097 return arr.astype(dtype, copy=True)
1098
1099 return arr.view(dtype)
ValueError: could not convert string to float: '-23,5327'```
也许你应该使用decimal=','
作为pd.read_csv
的参数:
df = pd.read_csv('data.csv', sep=';', decimal=',')
>>> df.select_dtypes(float)
17 22 23
0 17.5 -23.5327 -46.8182
1 56.3 -23.4315 -47.1269