Mahimai Raja J
Mahimai Raja J

Reputation: 106

How to label encode a attribute in pandas dataframe?

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?

Upvotes: 0

Views: 158

Answers (3)

ziying35
ziying35

Reputation: 1305

try this:

f, _ = pd.factorize(df['Result'])
df['Result'] = f
print(df)
>>>

  Result
0   0
1   1
2   1
3   0

Upvotes: 0

Muhammad Ali
Muhammad Ali

Reputation: 489

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})

Upvotes: 0

yeaske
yeaske

Reputation: 1432

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)

Upvotes: 1

Related Questions