有什么方法可以在Python pandas 中获取标签编码器的映射? [英] Any way to get mappings of a label encoder in Python pandas?

查看:76
本文介绍了有什么方法可以在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 

例如,

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

有什么办法可以知道周日已映射到3,周三已映射到6,依此类推?

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

推荐答案

最好的方法是使用sklearn库的标签编码器.

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

类似这样的东西:

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

这篇关于有什么方法可以在Python pandas 中获取标签编码器的映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆