Python Pandas.CategoricalDtype()
pandas.api.types.CategoricalDtype(categories = None, ordered = None) :该类对于指定独立于数值的分类数据的类型很有用,有类别和顺序。
参数-
categories :[类似索引]独特的类别分类。
ordered : [boolean] 如果是false,那么分类就被当作无序的。
返回-分类数据的类型规范
代码:
# Python code explaining
# numpy.pandas.CategoricalDtype()
# importing libraries
import numpy as np
import pandas as pd
from pandas.api.types import CategoricalDtype
a = CategoricalDtype(['a', 'b', 'c'], ordered=True)
print ("a : ", a)
b = CategoricalDtype(['a', 'b', 'c'])
print ("\nb : ", b)
print ("\nTrue / False : ", a == CategoricalDtype(['a', 'b', 'c'],
ordered=False))
c = pd.api.types.CategoricalDtype(categories=["a","b","d","c"], ordered=True)
print ("\nType : ", c)
c1 = pd.Series(['a', 'b', 'a', 'e'], dtype = c)
print ("c1 : \n", c1)
c2 = pd.DataFrame({'A': list('abca'), 'B': list('bccd')})
c3 = CategoricalDtype(categories=list('abcd'), ordered=True)
c4 = c2.astype(c3)
print ("\n c4['A'] : \n", c4['A'])