码迷,mamicode.com
首页 > 其他好文 > 详细

pandas的Categorical方法

时间:2018-09-09 20:06:07      阅读:2605      评论:0      收藏:0      [点我收藏+]

标签:使用   列表   print   self   flow   操作   转化   代码   col   

  对于数据样本的标签,如果我们事先不知道这个样本有多少类别,那么可以对数据集的类别列进行统计,这时我们用pandas的Categorical方法就非常快的实现。

1.说明: 

  你的数据最好是一个series,之后,pd.Categorical(series),得到分类的对象,使用categories或者codes进行查看

2.操作:

pd.Categorical( list ).codes 这样就可以直接得到原始数据的对应的序号列表,通过这样的处理可以将类别信息转化成数值信息 ,这样就可以应用到模型中去了 

代码:

  

 1 import tensorflow
 2 import lightgbm as lgb
 3 import pandas as pd
 4 
 5 
 6 class Deng(object):
 7     def __init__(self):
 8         pass
 9 
10     def main(self):
11         temp = [a, a, b, c, c]
12         st = pd.Categorical(temp)
13         print(st)
14         # [a, a, b, c, c]
15         # Categories(3, object): [a, b, c]
16 
17         # 遍历temp指出temp中每个字符所属类别的位置索引
18         st2 = st.codes
19         print(st2)
20         # [0 0 1 2 2]
21 
22 
23 if __name__ == __main__:
24     obj = Deng()
25     obj.main()

 

  

pandas的Categorical方法

标签:使用   列表   print   self   flow   操作   转化   代码   col   

原文地址:https://www.cnblogs.com/demo-deng/p/9614377.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!