Example :
Assume an attribute Result
Result
| M |
| B |
| B |
| M |
Into
Result
| 0 |
| 1 |
| 1 |
| 0 |
To achieve this without using LabelEncoder or OneHotEncoding. Like Is there any possible way to do it with the python dictionary?
CodePudding user response:
I would suggest creating a dictionary to map the values to new labels and use the map() method to label encode the attributes.
something like:
df['result'].map(mapping_dict)
CodePudding user response:
Something like this will work,
df = pd.DataFrame(data=[{'result': 'M'}, {'result':'B'}, {'result':'B'}, {'result':'M'}])
# Sol 1
def changeValue(r):
if r == 'M':
return 0
else:
return 1
df.apply(lambda x: changeValue(x['result']), axis=1)
# Sol 2
# df['result'].map({'M': 0, 'B': 1})