gpt4 book ai didi

python - 为什么我的对象没有转换为字符串?

转载 作者:太空宇宙 更新时间:2023-11-03 20:27:06 25 4
gpt4 key购买 nike

从 python 对象开始,当我尝试使用 astype(string).astype(float) 将字符串转换为 float 时,出现错误。

我使用正则表达式删除单位和空格,并删除带有 NA 的行。

df['Length'] = df['Length'].astype(str).astype(float)
<小时/>

ValueError Traceback(最近一次调用最后)

<ipython-input-137-724df1c0091a> in <module>
1 df['Length'] = df['Length'].astype(str).astype(float)
2 #df['Length'].astype(str).astype(float)
3 #df['Width'].astype(str).astype(float)

/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5689 # else, only a single dtype is given
5690 new_data = self._data.astype(dtype=dtype, copy=copy, errors=errors,
-> 5691 **kwargs)
5692 return self._constructor(new_data).__finalize__(self)
5693

/anaconda3/lib/python3.7/site-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):

/anaconda3/lib/python3.7/site-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

/anaconda3/lib/python3.7/site-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,

/anaconda3/lib/python3.7/site-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)

/anaconda3/lib/python3.7/site-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:无法将字符串转换为 float :

最佳答案

正如约翰指出的,错误在于将字符串转换为 float 。要目视检查空字符串,请使用 df['Length'] == '' 。要计算空字符串的数量,请使用: sum(df['Length'] == '')要删除包含空字符串的行,请使用:df = df[df['Length'] != ''] 。这将修改您的整个数据框,而不仅仅是 df['Length'] 。希望有帮助。

关于python - 为什么我的对象没有转换为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57742463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com