简体   繁体   English

使用 panda 将字符串“yes”转换为 1 但失败

[英]Using panda to convert string “yes” to 1 but failed

I'd like to convert "Yes"and "No" from column"ServiceLevel" to "1" and "0".我想将列“ServiceLevel”中的“Yes”和“No”转换为“1”和“0”。 This is my code:这是我的代码:

mydata['ServiceLevel'].replace(to_replace ='Yes',value = 1,inplace = 'True')

mydata['ServiceLevel'].replace(to_replace ='No', value = 0,inplace = 'True')

mydata['ServiceLevel'].head()

ValueError: For argument "inplace" expected type bool, received type str. ValueError:对于参数“就地”预期类型 bool,接收类型 str。

What's this mean?这是什么意思? How to correct it?如何纠正它?

inplace is a boolean argument - it takes either True or False , but you passed the string 'True' (note the quotes). inplace是一个 boolean 参数 - 它采用TrueFalse ,但您传递了字符串'True' (注意引号)。 Remove the quotes to get a boolean literal, and you should be fine:删除引号以获得 boolean 文字,你应该没问题:

mydata['ServiceLevel'].replace(to_replace ='Yes', value = 1, inplace = True)
mydata['ServiceLevel'].replace(to_replace ='No', value = 0, inplace = True)
# Here ---------------------------------------------------------------^---^

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

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