1

I'd like to convert "Yes"and "No" from column"ServiceLevel" to "1" and "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.

What's this mean? How to correct it?

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
Fiona
  • 11
  • 1
  • Does this answer your question? [Python Yes/No User Input](https://stackoverflow.com/questions/36018207/python-yes-no-user-input) – TAbdiukov Feb 13 '21 at 16:53
  • What about a `mydata["ServiceLevel"] = mydata["ServiceLevel"].apply(lambda x:True if x == 'Yes' else False)`, Inplace takes a boolean and not string ('True' is a string in your example) – Laurent R Feb 14 '21 at 02:23

1 Answers1

1

inplace is a boolean argument - it takes either True or False, but you passed the string 'True' (note the quotes). Remove the quotes to get a boolean literal, and you should be fine:

mydata['ServiceLevel'].replace(to_replace ='Yes', value = 1, inplace = True)
mydata['ServiceLevel'].replace(to_replace ='No', value = 0, inplace = True)
# Here ---------------------------------------------------------------^---^
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Please don't recommend the use of inplace - especially for Dataframe columns as it can lead to unintended side effects (most notably the setting with copy warning, or actually preventing the operation from updating the column in-place). See https://stackoverflow.com/a/60020384/4909087 – cs95 Feb 13 '21 at 15:34