【问题标题】:Why aren't my objects being converted to strings?为什么我的对象没有被转换为字符串?
【发布时间】:2019-09-01 00:38:35
【问题描述】:

从 python 对象开始,当我尝试使用 astype(string).astype(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:无法将字符串转换为浮点数:

【问题讨论】:

  • 最后一条错误消息表明该值为空字符串。是吗?此外,它 正在 被转换为字符串;错误出现在随后尝试转换为浮点数时。
  • 我正在处理一个大型数据集,我看到的前 100 个值不是空字符串。您如何建议测试和删除空白字符串?

标签: python


【解决方案1】:

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

【讨论】:

  • 效果很好!我有一排空白字符串,现在它消失了,一切正常。非常感谢!
  • 很高兴为您提供帮助!如果它解决了你的问题,你可以接受我的回答。 (请看这里:stackoverflow.com/help/someone-answers
猜你喜欢
  • 1970-01-01
  • 2021-02-23
  • 1970-01-01
  • 2019-10-11
  • 2020-04-12
  • 1970-01-01
  • 2022-01-21
  • 2012-12-07
  • 2011-02-15
相关资源
最近更新 更多