pandas.Categorical(val,category = None,ordered = None,dtype = None):它代表一个分类变量。分类是一种 Pandas 数据类型,它对应于统计数据中的分类变量。这样的变量具有固定且有限数量的可能值。例如-等级,性别,血型类型等。
同样,在分类变量的情况下,逻辑顺序与分类数据不同,例如“one”,“two”,“three”。但是这些变量的排序使用逻辑顺序。
Parameters-
val :[list-like] The values of categorical. categories:[index like] Unique categorisation of the categories. ordered :[boolean] If false, then the categorical is treated as unordered. dtype :[CategoricalDtype] an instance.Error-
ValueError: If the categories do not validate. TypeError : If an explicit ordered = True but categorical can't be sorted. Return- Categorical varibale
代码:
# Python code explaining
# numpy.pandas.Categorical()
# importing libraries
import numpy as np
import pandas as pd
# Categorical using dtype
c = pd.Series(["a", "b", "d", "a", "d"], dtype ="category")
print ("\nCategorical without pandas.Categorical():\n", c)
c1 = pd.Categorical([1, 2, 3, 1, 2, 3])
print ("\n\nc1:", c1)
c2 = pd.Categorical(['e', 'm', 'f', 'i',
'f', 'e', 'h', 'm' ])
print ("\nc2:", c2)
输出:
# Ordered = True
c3 = pd.Categorical(['e', 'm', 'f', 'i',
'f', 'e', 'h', 'm' ], ordered = True)
print ("\nc3:", c3)
输出:
# Mixed categories
c4 = pd.Categorical(['a', 2, 3, 1, 2, 3])
print ("\nc4:", c4)
c5 = pd.Categorical(['a', 2, 3, 1, 2, 3], ordered = True)
print ("\nc5:", c5)
输出:
# using categories attribute
c6 = pd.Categorical([1, 2, 3, 1, 2, 3], categories = [4, 1, 3, 5])
print ("\nc6:", c6)
print("\n\nSeries:\n", pd.Series(c6))
df = pd.DataFrame({"A":[1, 2, 3, 1, 2, 3]})
df["B"] = c6
print ("\n\nDataframe:\n", df)
输出:
相关用法
注:本文由纯净天空筛选整理自Mohit Gupta_OMG 大神的英文原创作品 Python | Pandas.Categorical()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。