简体   繁体   中英

How could I convert this column to numeric?

I have a problem with to convert this column to numeric. I tried this

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.

I don't know what to do. 在此处输入图像描述

You can replace the \xa0 unicode character with an empty space before converting to int .

import pandas as pd

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

0     13312
1    141234
dtype: int64

You can also use unicodedata.normalize to normalize the unicode character to a space, then replace the space with empty space, and finally convert to int .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM