當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python Pandas.Categorical()用法及代碼示例


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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。