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


Python pandas.Categorical.from_codes用法及代碼示例

用法:

classmethod Categorical.from_codes(codes, categories=None, ordered=None, dtype=None)

從代碼和類別或 dtype 中創建分類類型。

如果您已經有代碼和類別/dtype,則此構造函數很有用,因此不需要(計算密集型)分解步驟,這通常在構造函數上完成。

如果您的數據不遵循此約定,請使用普通構造函數。

參數

codesarray-like of int

一個整數數組,其中每個整數指向 categories 或 dtype.categories 中的一個類別,否則為 -1 表示 NaN。

categoriesindex-like,可選

類別的類別。項目必須是唯一的。如果此處未提供類別,則必須在 dtype 中提供類別。

ordered布爾型,可選

此分類是否被視為有序分類。如果未在此處或 dtype 中給出,則生成的分類將是無序的。

dtypeCategoricalDtype 或“category”,可選

如果 CategoricalDtype ,不能與 categoriesordered 一起使用。

返回

分類的

例子

>>> dtype = pd.CategoricalDtype(['a', 'b'], ordered=True)
>>> pd.Categorical.from_codes(codes=[0, 1, 0, 1], dtype=dtype)
['a', 'b', 'a', 'b']
Categories (2, object): ['a' < 'b']

相關用法


注:本文由純淨天空篩選整理自pandas.pydata.org大神的英文原創作品 pandas.Categorical.from_codes。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。