【问题标题】:The astype function is not working mysteriouslyastype 函数没有神秘地工作
【发布时间】:2019-12-16 17:42:34
【问题描述】:

所以我试图将这些值转换为浮点数以便能够求和()。问题是有一些奇怪的东西让我无法完成它

数据:

cw= pd.DataFrame({ "campaign": "151515151515" , 
                   "Media_Cost":  "$ 14,52" })


cw.dtypes

Media_Cost       object

我的尝试, 我尝试了下面的所有代码行,当时一个,都没有神秘地工作..

cw["Media_Cost"] = cw["Media_Cost"].str.replace('$','')

# Attempt 1
cw.Media_Cost = cw.Media_Cost.astype(float)

# Attempt 3
cw.Media_Cost = len(float(cw.Media_Cost))

# Attempt 4
cw.Media_Cost = cw.Media_Cost.apply(lambda x: float(cw.Media_Cost))

错误仍然存​​在..

cw["Media_Cost"] = cw["Media_Cost"].str.replace('$','').str.replace(',', '.').astype(float)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-382-f5688d76abed> in <module>
      1 # cw.Media_Cost = cw.Media_Cost.apply(lambda x: float(cw.Media_Cost))
----> 2 cw["Media_Cost"] = cw["Media_Cost"].str.replace('$','').str.replace(',', '.').astype(float)
      3 
      4 # cw.Media_Cost = float(cw.Media_Cost)
      5 

~\Anaconda3\lib\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\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\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\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\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\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: could not convert string to float: '1.443.48'

【问题讨论】:

  • 是的,在 Python 中,`'1,443.48'` 不代表浮点数。浮点文字不能有数千个分隔符。继续清洁你的琴弦
  • 请注意,您可以将babel.numbers.parse_decimal 与(例如)德语语言环境一起使用来处理这样的本地化数字。会比 Python 默认期望的“C 格式”浮点数慢得多
  • @Peter 刚刚注意到您来自葡萄牙,并且像德国一样使用相同的逗号作为小数点,因此您可以将 locale='pt' 与 babel 一起使用
  • @SamMason 是的,我在看'1.443.48' 我现在看到了问题。无论如何,可能你必须使用类似:stackoverflow.com/questions/40717037/…
  • @juanpa.arrivillaga 是的,1.443.48 确实坏了!这个问题现在看起来与stackoverflow.com/a/22137890/1358308 非常相似,OP 应该去掉$s 然后按照那个答案

标签: python pandas


【解决方案1】:

你可以试试:

cw = pd.DataFrame({"campaign": "151515151515", "Media_Cost":  "$ 1,443.48" }, index=[0])
cw["Media_Cost"] = cw["Media_Cost"].str.replace('$','').str.replace(',', '').astype(float)
cw.dtypes

结果:

campaign       object
Media_Cost    float64
dtype: object

【讨论】:

  • 我更新了问题,我尝试了代码它仍然给出了一些错误。
猜你喜欢
  • 2018-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-05
  • 2020-10-24
  • 1970-01-01
  • 2014-12-08
  • 2020-12-03
相关资源
最近更新 更多