将字符串转换为浮点数:ValueError:无法将字符串转换为浮点数:“。”

3

我正在尝试将字符串转换为浮点数,但是我得到了标题中的错误。我不明白为什么它不将句点(“.”)识别为十进制。这是我的数据帧的头部。

      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')

I get:

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

非常感谢您的帮助和建议。
更新:我不小心把NAs“..”转换成了“。”。现在我已经将它们转换为“”。我现在得到了。
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 可以直接表示为 .。你希望如何处理这些数据呢? - undefined
使用pd.to_numeric - undefined
谢谢大家。我都尝试过了。我已经更新了我的原始帖子。 - undefined
无法弄清楚,但在R中没有遇到任何问题:growth_raw[,3:11] = lapply(growth_raw[,3:11], as.numeric) - undefined
2个回答

6
使用pd.to_numeric函数并添加参数errors='coerce'可以更安全地处理数据(因为实际数据中可能存在一些错误)。例如:
df.iloc[:,3:].apply(pd.to_numeric,errors='coerce')

谢谢。已经尝试过了。 - undefined

0

这个数据示例和你转换它的方式似乎没有问题。 所以导致问题的原因应该是数据中的其他地方。

我不小心把 NAs '..' 转换成了 '.',现在我已经把它们转换成了 ''。

为什么你这样做?我不明白。你认为 pandas 怎么可能将空字符串 '' 转换为浮点数呢?在交互模式下尝试一下 float(''),你会得到你这里报告的错误。 只需让 NaNs 保持原样,看看会发生什么。

请你也提供完整的错误回溯信息好吗?看起来在应该是数字的地方你有一个 '.'。


由于我将'..'识别为NA值,所以进行了转换,但是出现了以下错误:ValueError: could not convert string to float: '..'。错误的完整追踪如下: Traceback (most recent call last): File "C:\Users\user\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-30-18b4e27ef82a>", line 1, in <module> growth_raw.iloc[:,3].values.astype('float64') ValueError: could not convert string to float: '..' - undefined
@Minsky 你的原始数据是以csv格式存储的吗?如果是的话,那么你就不需要将双点转换为NaNs,pandas可以为你完成这个操作。只需使用df = pd.read_csv(path_to_data, na_values='..')加载数据,你将得到一个包含可转换为浮点数的数据框。要转换数据,你可以使用applymapconvert_objects方法。 - undefined
@Minsky 如果这条建议对你有帮助的话,我会把它作为我的答案,以便其他遇到类似问题的人也可以使用。所以,请回复一下吧。 - undefined
抱歉耽搁了。是的,我确实注意到了,但我想保持这些值的独立性,因为我还有一些额外的'NaN'行,我希望在将双点转换为'NaN'之前能够轻松删除它们。在这样做之后,我使用growth_raw.apply(lambda x: x.str.strip())去除了字符串,并且转换工作正常。谢谢你建议不要将任何东西转换为''。那真的很有帮助。我曾以为pandas可以将其识别为NaN,因为它经常用于去除空格。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接