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并删除空格