Search code examples
pythonpandascategorical-data

Any way to get mappings of a label encoder in Python pandas?


I am converting strings to categorical values in my dataset using the following piece of code.

data['weekday'] = pd.Categorical.from_array(data.weekday).labels 

For eg,

index    weekday
0        Sunday
1        Sunday
2        Wednesday
3        Monday
4        Monday
5        Thursday
6        Tuesday

After encoding the weekday, my dataset appears like this:

index    weekday
    0       3
    1       3
    2       6
    3       1
    4       1
    5       4
    6       5

Is there any way I can know that Sunday has been mapped to 3, Wednesday to 6 and so on?


Solution

  • The best way of doing this can be to use label encoder of sklearn library.

    Something like this:

    from sklearn import preprocessing
    le = preprocessing.LabelEncoder()
    le.fit(["paris", "paris", "tokyo", "amsterdam"])
    list(le.classes_)
    le.transform(["tokyo", "tokyo", "paris"])
    list(le.inverse_transform([2, 2, 1]))