首页 > 解决方案 > 由于“不可见”字符,如何将列转换为带有错误的数字

问题描述

我有一个由 pd.read_html 导入的 DF。此列是一个对象。看起来像这样 - 很简单吧?

Column
6 600
7 000
6 700

我要做的是:将其转换为数字我收到一个错误'无法在位置 0 解析字符串“6 600”'这就是为什么我决定删除空格字符''以准备将其转换为数字柱子。

我试过的:

df['Column'] = df['Column'].str.replace(' ','', regex = True) 
#==> Nothing changes, it stays like it was. And I still can't move it to numeric.

我也试过:

df['Column'] = df['Column'].map(lambda x:x.split(' ')[0])
#==> The same outcome like above

当我尝试将其转换为数字时,我收到这样的错误:

df['Column'] = df['Column'].astype({'Column': 'int'})
#==> invalid literal for int() with base 10: '6\xa0600'

当我这样做时......

df['Column'] = pd.to_numeric(df['Column'])
#==> Unable to parse string "6 600" at position 0

据我了解,它必须与类型相关联,显然我做错了什么,但我无法理解它是什么。第一个提到的方法(str.replace)中有趣的是我导入的其他数据是可以的。

任何人都可以帮我解决它吗?

标签: pythonpandasreplacetype-conversion

解决方案


推荐阅读