Pandas-24. Category

Pandas-24. Category

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

创建Category对象

指定dtype

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的构造函数:

pandas.Categorical(values, categories, ordered)

只有值参数:

cat = pd.Categorical(['a', 'b', 'c', 'a', 'b', 'c'])
print(cat)
'''
[a, b, c, a, b, c]
Categories (3, object): [a, b, c]
'''

值参数+类别参数:

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参数:

 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的基础信息。

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对象的属性:

print(cat.categories)
'''
Index(['b', 'a', 'c'], dtype='object')
'''

顺序

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

print(cat.ordered)
'''
False # 未指定顺序
'''

重命名类别

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

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()方法可以追加新类别:

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()方法可以删除类别:

print(cat.remove_categories("a"))
'''
[NaN, c, c, NaN]
Categories (3, object): [b, c, d]
'''

比较类别

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

  • ==!=比较和Category相同长度的类似列表的对象
  • ==!=>>=<<=比较两个Category
  • 比较Category和标量
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
'''
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,145评论 1 32
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,184评论 6 13
  • 37.cocoa内存管理规则 1)当你使用new,alloc或copy方法创建一个对象时,该对象的保留计数器值为1...
    如风家的秘密阅读 893评论 0 4
  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,788评论 0 9
  • ORA-00001: 违反唯一约束条件 (.) 错误说明:当在唯一索引所对应的列上键入重复值时,会触发此异常。 O...
    我想起个好名字阅读 5,451评论 0 9