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

前端 未结 8 1888
清酒与你
清酒与你 2021-02-01 15:11

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         


        
8条回答
  •  被撕碎了的回忆
    2021-02-01 15:35

    You can create additional dictionary with mapping:

    from sklearn import preprocessing
    le = preprocessing.LabelEncoder()
    le.fit(data['name'])
    le_name_mapping = dict(zip(le.classes_, le.transform(le.classes_)))
    print(le_name_mapping)
    {'Tom': 0, 'Nick': 1, 'Kate': 2}
    

提交回复
热议问题