sinha-shaurya
sinha-shaurya

Reputation: 577

Label Encoding for nominal values

I am working on dataset which has a column which is nominal i.e categorical in nature, somewhat like this:

>>>data['MODE'].unique()
array(['A', 'B', 'C'], dtype=object)

I would like to convert these nominal values to numerical counterparts(A-0,B-1,C-2). How do I do this ?


The code given is just to demonstrate the classes present in the column, I need to convert the entire column to ordinal (that is map A->0,B->1,C->2)

Upvotes: 0

Views: 175

Answers (1)

jezrael
jezrael

Reputation: 862641

Use factorize:

a = data['MODE'].unique()
print (pd.factorize(a)[0])
[0 1 2]


print (dict(zip(a, pd.factorize(a)[0])))
{'A': 0, 'B': 1, 'C': 2}

Or enumerate:

print ({v:k for k, v in dict(enumerate(a)).items()})
{'A': 0, 'B': 1, 'C': 2}

Upvotes: 1

Related Questions