前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Pandas-24. Category

Pandas-24. Category

作者头像
悠扬前奏
发布2019-05-29 20:38:57
6160
发布2019-05-29 20:38:57
举报

Pandas-24. Category

  • Category是Pandas数据类型
  • 只能采用有限的数据量,通常是固定长度
  • 可能有顺序,但不能执行数字操作
  • 使用场景:
    • 字符串变量只包含几个不同的值,将这样的字符串变量转换为Category可以节省内存
    • 变量的词汇顺序与逻辑顺序不同("one", "two", "three"),通过转换为Category并指定顺序,排序和最小最大等操作将使用逻辑顺序,而不是词法顺序。
    • 作为其他python库的信号,该列应该作为一个分类变量(例如plot)

创建Category对象

指定dtype

代码语言:javascript
复制
import pandas as pd
s = pd.Series(["a","b","c","a"], dtype="category")
print (s)
'''
0    a
1    b
2    c
3    a
dtype: category
Categories (3, object): [a, b, c]
'''

注意Category只有三个了。

pd.Categorical

Category的构造函数:

代码语言:javascript
复制
pandas.Categorical(values, categories, ordered)

只有值参数:

代码语言:javascript
复制
cat = pd.Categorical(['a', 'b', 'c', 'a', 'b', 'c'])
print(cat)
'''
[a, b, c, a, b, c]
Categories (3, object): [a, b, c]
'''

值参数+类别参数:

代码语言:javascript
复制
cat=pd.Categorical(['a','b','c','a','b','c','d'], ['c', 'b', 'a'])
print (cat)
'''
[a, b, c, a, b, c, NaN]
Categories (3, object): [c, b, a]
'''

第二个参数是类别参数,在类别参数中不存在的值视为NaN

加上ordered参数:

代码语言:javascript
复制
 cat=pd.Categorical(['a','b','c','a','b','c','d'], ['c', 'b', 'a'],ordered=True)
print (cat)
'''
[a, b, c, a, b, c, NaN]
Categories (3, object): [c < b < a]
'''

逻辑排序上a>b> c

描述

Category对象的describe()函数,返回对Category的基础信息。

代码语言:javascript
复制
cat = pd.Categorical(["a", "c", "c", np.nan], categories=["b", "a", "c"])
print(cat.describe())
'''
count     3
unique    2
top       c
freq      2
Name: cat, dtype: object
'''

Category的属性

categories包含了Category对象的属性:

代码语言:javascript
复制
print(cat.categories)
'''
Index(['b', 'a', 'c'], dtype='object')
'''

顺序

ordered属性包含了Category对象的顺序:

代码语言:javascript
复制
print(cat.ordered)
'''
False # 未指定顺序
'''

重命名类别

通过将新值重新分配categories属性,可以重命名类别:

代码语言:javascript
复制
print (cat.categories)
cat.categories = ["Group %s" % g for g in s.cat.categories]
print(cat.categories)
'''
Index(['b', 'a', 'c'], dtype='object')
Index(['Group a', 'Group b', 'Group c'], dtype='object')
'''

附加新类别

add_categories()方法可以追加新类别:

代码语言:javascript
复制
cat = pd.Categorical(["a", "c", "c", 'a'], categories=["b", "a", "c"])
cat = cat.add_categories("d")
print(cat)
'''
[a, c, c, a]
Categories (4, object): [b, a, c, d]
'''

删除类别

remove_categories()方法可以删除类别:

代码语言:javascript
复制
print(cat.remove_categories("a"))
'''
[NaN, c, c, NaN]
Categories (3, object): [b, c, d]
'''

比较类别

三种情况下可以将Category和其他类型进行比较:

  • ==!=比较和Category相同长度的类似列表的对象
  • ==!=>>=<<=比较两个Category
  • 比较Category和标量
代码语言:javascript
复制
cat = pd.Series([1,2,3]).astype("category", categories=[1,2,3], ordered=True)
cat1 = pd.Series([2,2,2]).astype("category", categories=[1,2,3], ordered=True)
print(cat > cat1)
print("----------")
print(cat == [1,2,3])
print("----------")
print(cat > 2)
'''
0    False
1    False
2     True
dtype: bool
----------
0    True
1    True
2    True
dtype: bool
----------
0    False
1    False
2     True
dtype: bool
'''
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.04.04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Pandas-24. Category
    • 创建Category对象
      • 指定dtype
      • pd.Categorical
      • 描述
      • Category的属性
      • 顺序
      • 重命名类别
      • 附加新类别
      • 删除类别
      • 比较类别
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档