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
'''