简体   繁体   English

将对象列转换为数字

[英]Convert Object column to number

I am currently working with a python dataframe and there is a names column with different names in the form of strings.我目前正在使用 python 数据框,并且有一个名称列,其名称以字符串的形式存在。

I try to convert these names into individual numbers so for example:我尝试将这些名称转换为单独的数字,例如:

"Paul" 1
"John" 2
"Nina" 3
...

I already tried我已经试过了

dataset['name'] = dataset['name'].astype(np.int64)

and this:和这个:

dataset = dataset.astype({'collection_author':'int'})

Which both lead to the following error:这两者都导致以下错误:

ValueError: invalid literal for int() with base 10

and also this:还有这个:

dataset['name'] = pd.to_numeric(dataset['name'])

which leads to:这导致:

ValueError: Unable to parse string "John" at position 0

If I add the errors='coerce' the code runs through however most of the values are replaced with NaN.如果我添加errors='coerce'代码会运行,但是大多数值都替换为 NaN。

使用pandas.get_dummiessklearn.preprocessing.OneHotEncoder将字符串转换为整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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