Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将此列转换为数字?_Python_Pandas_Type Conversion - Fatal编程技术网

Python 如何将此列转换为数字?

Python 如何将此列转换为数字?,python,pandas,type-conversion,Python,Pandas,Type Conversion,我无法将此列转换为数字。我试过这个 pricesquare['PRICE_SQUARE'] = pd.to_numeric(pricesquare['PRICE_SQUARE']) ValueError: Unable to parse string "13 312 " at position 0 df["PRICE_SQUARE"] = df["PRICE_SQUARE"].astype(str).astype(int) Val

我无法将此列转换为数字。我试过这个

pricesquare['PRICE_SQUARE'] = 
pd.to_numeric(pricesquare['PRICE_SQUARE'])
ValueError: Unable to parse string "13 312 " at position 0

df["PRICE_SQUARE"] = df["PRICE_SQUARE"].astype(str).astype(int) 
ValueError: invalid literal for int() with base 10: '13\xa0312.

我不知道该怎么办。

在转换为
int
之前,您可以将
\xa0
unicode字符替换为一个空格

import pandas as pd

data = ["13\xa0312", "14\xa01234"]
pd.Series(data).str.replace("\xa0", "").astype(int)

0     13312
1    141234
dtype: int64
import unicodedata
import pandas as pd

data = ["13\xa0312", "14\xa01234"]
pd.Series(data).apply(lambda s: unicodedata.normalize("NFKC", s)).str.replace(" ", "").astype(int)

0     13312
1    141234
dtype: int64
您还可以使用将unicode字符规范化为空格,然后将空格替换为空空格,最后转换为
int

import pandas as pd

data = ["13\xa0312", "14\xa01234"]
pd.Series(data).str.replace("\xa0", "").astype(int)

0     13312
1    141234
dtype: int64
import unicodedata
import pandas as pd

data = ["13\xa0312", "14\xa01234"]
pd.Series(data).apply(lambda s: unicodedata.normalize("NFKC", s)).str.replace(" ", "").astype(int)

0     13312
1    141234
dtype: int64
在转换为int之前,在函数中使用apply并删除空格