Python 为什么不';我的对象是否正在转换为字符串?

Python 为什么不';我的对象是否正在转换为字符串?,python,Python,从一个python对象开始,当我尝试使用astype(string).astype(float)将字符串转换为浮点值时,出现了一个错误 我使用正则表达式删除了单位和空格,并删除了带有NA的行 df['Length'] = df['Length'].astype(str).astype(float) ValueError回溯(最近一次调用上次) in 1 df['Length']=df['Length'].astype(str).astype(float) 2#df['Length'].ast

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

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

df['Length'] = df['Length'].astype(str).astype(float)

ValueError回溯(最近一次调用上次)

in
1 df['Length']=df['Length'].astype(str).astype(float)
2#df['Length'].astype(str).astype(float)
3#df['Width'].astype(str).astype(float)
/astype中的anaconda3/lib/python3.7/site-packages/pandas/core/generic.py(self、dtype、copy、errors、**kwargs)
5689#否则,只给出一个数据类型
5690新数据=自身。_数据.astype(dtype=dtype,copy=copy,errors=errors,
->5691**夸尔格)
5692返回self.\u构造函数(新数据)。\u完成\u(self)
5693
/astype中的anaconda3/lib/python3.7/site-packages/pandas/core/internals/managers.py(self,dtype,**kwargs)
529
530 def astype(自我,数据类型,**kwargs):
-->531返回self.apply('astype',dtype=dtype,**kwargs)
532
533 def转换(自身,**kwargs):
/应用中的anaconda3/lib/python3.7/site-packages/pandas/core/internal/managers.py(self、f、axes、filter、do\u integrity\u check、consolidate、**kwargs)
393拷贝=对齐(拷贝)
394
-->395应用=getattr(b,f)(**kwargs)
396结果块=\u扩展块(应用,结果块)
397
/astype中的anaconda3/lib/python3.7/site-packages/pandas/core/internals/blocks.py(self、dtype、copy、errors、values、**kwargs)
532 def astype(self、dtype、copy=False、errors='raise',values=None、**kwargs):
533返回self.\u astype(数据类型,复制=复制,错误=错误,值=值,
-->534**夸尔格)
535
536 def_astype(self,dtype,copy=False,errors='raise',values=None,
/anaconda3/lib/python3.7/site-packages/pandas/core/internal/blocks.py in_astype(self、dtype、copy、errors、values、**kwargs)
631
632#u astype_nansafe仅适用于1-d
-->633 values=astype_nansafe(values.ravel(),dtype,copy=True)
634
635#待办事项(分机)
/astype_nansafe中的anaconda3/lib/python3.7/site-packages/pandas/core/dtypes/cast.py(arr、dtype、copy、skipna)
700如果复制或是对象类型(arr)或是对象类型(dtype):
701#显式复制,或者是必需的,因为NumPy无法从/到对象查看。
-->702返回arr.astype(dtype,copy=True)
703
704返回arr.view(数据类型)

ValueError:无法将字符串转换为浮点:

正如John指出的,错误在于将字符串转换为浮点。 要目视检查空字符串,请使用
df['Length']=''
。 要计算空字符串的数量,请使用:
sum(df['Length']='')
要删除带有空字符串的行,请使用:
df=df[df['Length']!='']
。这将修改整个数据帧,而不仅仅是
df['Length']

希望能有所帮助。

正如John指出的,错误在于将字符串转换为浮点。 要目视检查空字符串,请使用
df['Length']=''
。 要计算空字符串的数量,请使用:
sum(df['Length']='')
要删除带有空字符串的行,请使用:
df=df[df['Length']!='']
。这将修改整个数据帧,而不仅仅是
df['Length']

希望有帮助。

最后一条错误消息表明该值为空字符串。是吗?同时,它正在转换为字符串;错误发生在随后尝试转换为浮点值时。我正在处理一个大型数据集,我看到的前100个值不是空字符串。您建议如何测试和删除空字符串?Tha最后一条错误消息表明该值是空字符串。是吗?另外,它正在转换为字符串;错误发生在随后尝试转换为浮点值时。我正在处理一个大型数据集,并且我看到的前100个值不是空字符串。您建议如何测试和删除空字符串?这非常有效!我知道了d一行空白字符串,现在它不见了,一切正常。非常感谢!很高兴帮助!如果它解决了您的问题,您可以接受我的答案。(请看这里:)很好!我有一行空白字符串,现在已经没有了,一切都正常了。非常感谢!很高兴能帮助你!如果它解决了你的问题,你可以接受我的答案。(请看这里:)
<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)