Search code examples
pythonpandasdataframeencodingcategorical-data

Python: replace values of a categorical variable to something else in a data frame


i have a pandas data frame in which there's a column named label of categorical type having three categories as ('>5' , '<30' , 'NO'). I want to change ('>5' , '<30') these two categories to 'yes' and i can't seem to figure out how. I want to do this with python and also with dtale (python package).

I have managed to do it in python this way:

label_changed = {"label": {">5": "YES", "<30": "YES"}}
bp = bp.replace(label_changed)

Is there any other, more efficient, way to do this?

Also I still haven't managed to do this using dtale.


Solution

  • You can use replace() and pass a list with the values to be replaced and then the parameter with replacement, it's a bit tidier when you want to replace multiple values with a unique one:

    to_replace = [">5","<30"]
    bp = bp.replace(to_replace,"Yes")