Python Pandas.Categorical()

Python Pandas.Categorical()

pandas.Categorical(val, categories = None, ordered = None, dtype = None) :它代表一个分类变量。分类是一种pandas数据类型,对应于统计学中的分类变量。这种变量具有固定的和有限的可能值。例如–成绩、性别、血型等。
另外,在分类变量的情况下,逻辑顺序与分类数据不一样,例如,”一”、”二”、”三”。但这些变量的排序使用了逻辑顺序。

参数-

val : [类似列表] 分类的值。
categories : [index like] 类别的唯一分类。
ordered : [boolean] 如果是false,那么分类被视为无序的。
dtype : [CategoricalDtype] 一个实例。

Error- ValueError : 如果分类没有得到验证。
TypeError : 如果一个明确的有序=真,但分类不能被排序。

返回- 分类变量

代码:

# 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)

输出 :

Python  Pandas.Categorical()

# Ordered = True
c3 = pd.Categorical(['e', 'm', 'f', 'i',
                     'f', 'e', 'h', 'm' ], ordered = True)
print ("\nc3 : ", c3)

输出 :

Python  Pandas.Categorical()

# 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)

输出 :

Python  Pandas.Categorical()

# 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)

输出 :

Python  Pandas.Categorical()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程