【问题标题】:Converting strings to floats: ValueError: could not convert string to float: '.'将字符串转换为浮点数:ValueError:无法将字符串转换为浮点数:'.'
【发布时间】:2017-10-24 03:53:25
【问题描述】:

我正在尝试将字符串转换为浮点数,但标题中出现错误。我不明白为什么它不将句点('.')识别为小数。这是我的数据框的头部。

      Country                                           Variable  \
0  Afghanistan                 Inflation, GDP deflator (annual %)   
1  Afghanistan                            GDP (constant 2010 US$)   
2  Afghanistan                                  Population, total   
3  Afghanistan                       Population ages 15-64, total   
4  Afghanistan  Employment to population ratio, 15+, total (%)...   

2007 [YR2007]     2008 [YR2008]      2009 [YR2009]     2010 [YR2010]  \
0  22.3820157780035  2.17910328500052  -2.10708255443797  9.43779477259656   
1  11721187594.2052    12144482858.18   14697331940.6464  15936800636.2487   
2          26616792          27294031           28004331          28803167   
3          13293041          13602366           13950492          14372378   
4  47.1220016479492  47.0480003356934    47.015998840332  47.0429992675781   

这里是代码(Python 3.6):

growth_raw.iloc[:,3:] = growth_raw.iloc[:,3:].values.astype('float64')

我明白了:

ValueError: could not convert string to float: '.'

任何明智的想法都值得赞赏。非常感谢。

更新:我不小心将 NA '..' 转换为 '.'。我现在已将它们转换为''。我现在明白了

ValueError: could not convert string to float:

我试过了

growth_raw.apply(lambda x: x.str.strip())

为了转换,我试过了

growth_raw.iloc[:,2:].values.astype(float)

这给了我上述错误。我还尝试了以下两种方法,它们没有给我任何错误,但对数据什么也不做:

growth_raw.iloc[:,2:].apply(lambda x: pd.to_numeric(x), axis=0)
growth_raw.iloc[:,2:].apply(pd.to_numeric,errors='coerce')

【问题讨论】:

  • 似乎0.0 可能只表示为.。你想如何处理这些数据?
  • 使用pd.to_numeric
  • 谢谢大家。两个都试过了。我已经更新了我原来的帖子。
  • 想不通,但用 R 没问题:growth_raw[,3:11] = lapply(growth_raw[,3:11], as.numeric)

标签: python python-3.x pandas


【解决方案1】:

使用 pd.to_numeric 以更安全地使用 erros = 'coerce' (实际可能存在一些不良数据),即

df.iloc[:,3:].apply(pd.to_numeric,errors='coerce')

【讨论】:

  • 谢谢。试过了。
【解决方案2】:

这个数据样本似乎没有任何问题,您转换它的方式对我来说很好。 所以导致问题的原因是数据中的其他地方。

我不小心将 NA '..' 转换为 '.'。我现在已将它们转换为''。

你为什么这样做?我无法得到它。你认为熊猫应该如何将''(空字符串)转换为浮点数。在交互模式下试试这个float(''),你会得到你在这里报告的错误。 离开NaNs,看看会发生什么。

能否请您提供错误的完整回溯?看起来你有'。它应该是一个数字。

【讨论】:

  • 我已经完成了转换,因为我将 NA 设为 '..' 并且收到 ValueError: could not convert string to float: '..' 错误的完整回溯如下: Traceback (最近一次调用最后一次):文件“C:\Users\user\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py”,第 2881 行,在 run_code exec(code_obj, self.user_global_ns, self.user_ns) 文件中“”,第 1 行,在 growth_raw.iloc[:,3].values.astype('float64') ValueError: could not convert string to float: '..'
  • @Minsky 你的原始数据在 csv 中吗?如果是这样,那么您不必将双点转换为NaNs,pandas 可以为您执行此操作。只需将其加载为df = pd.read_csv(path_to_data, na_values='..'),您将获得一帧浮点可转换字符串。要转换数据,您可以使用applymapconvert_objects
  • @Minsky 如果这条建议有帮助,那么我会将其作为我的答案,以便其他有类似问题的人也可以使用它。所以请注意回复。
  • 抱歉耽搁了。是的,我实际上已经注意到了这一点,但我想保持这些值不同,因为我还有一些额外的“NaN”行,我想在将双点转换为“NaN”之前轻松删除这些行。这样做之后,我用 growth_raw.apply(lambda x: x.str.strip()) 剥离了字符串,并且转换工作正常。感谢您建议不要将任何内容转换为“”。这很有帮助。我曾假设 pandas 可以将其读取为 NaN,因为它通常用于剥离空格。
猜你喜欢
  • 2019-03-23
  • 2018-06-13
  • 2013-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-04
  • 2019-12-19
相关资源
最近更新 更多