Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.Categorical#

classpandas.Categorical(values,categories=None,ordered=None,dtype=None,fastpath=<no_default>,copy=True)[source]#

Represent a categorical variable in classic R / S-plus fashion.

Categoricals can only take on a limited, and usually fixed, numberof possible values (categories). In contrast to statistical categoricalvariables, aCategorical might have an order, but numerical operations(additions, divisions, …) are not possible.

All values of theCategorical are either incategories ornp.nan.Assigning values outside ofcategories will raise aValueError. Orderis defined by the order of thecategories, not lexical order of thevalues.

Parameters:
valueslist-like

The values of the categorical. If categories are given, values not incategories will be replaced with NaN.

categoriesIndex-like (unique), optional

The unique categories for this categorical. If not given, thecategories are assumed to be the unique values ofvalues (sorted, ifpossible, otherwise in the order in which they appear).

orderedbool, default False

Whether or not this categorical is treated as a ordered categorical.If True, the resulting categorical will be ordered.An ordered categorical respects, when sorted, the order of itscategories attribute (which in turn is thecategories argument, ifprovided).

dtypeCategoricalDtype

An instance ofCategoricalDtype to use for this categorical.

Attributes

categories

The categories of this categorical.

codes

The category codes of this categorical index.

ordered

Whether the categories have an ordered relationship.

dtype

TheCategoricalDtype for this instance.

Methods

from_codes(codes[, categories, ordered, ...])

Make a Categorical type from codes and categories or dtype.

__array__([dtype, copy])

The numpy array interface.

Raises:
ValueError

If the categories do not validate.

TypeError

If an explicitordered=True is given but nocategories and thevalues are not sortable.

See also

CategoricalDtype

Type for categorical data.

CategoricalIndex

An Index with an underlyingCategorical.

Notes

See theuser guidefor more.

Examples

>>>pd.Categorical([1,2,3,1,2,3])[1, 2, 3, 1, 2, 3]Categories (3, int64): [1, 2, 3]
>>>pd.Categorical(['a','b','c','a','b','c'])['a', 'b', 'c', 'a', 'b', 'c']Categories (3, object): ['a', 'b', 'c']

Missing values are not included as a category.

>>>c=pd.Categorical([1,2,3,1,2,3,np.nan])>>>c[1, 2, 3, 1, 2, 3, NaN]Categories (3, int64): [1, 2, 3]

However, their presence is indicated in thecodes attributeby code-1.

>>>c.codesarray([ 0,  1,  2,  0,  1,  2, -1], dtype=int8)

OrderedCategoricals can be sorted according to the custom orderof the categories and can have a min and max value.

>>>c=pd.Categorical(['a','b','c','a','b','c'],ordered=True,...categories=['c','b','a'])>>>c['a', 'b', 'c', 'a', 'b', 'c']Categories (3, object): ['c' < 'b' < 'a']>>>c.min()'c'

[8]ページ先頭

©2009-2025 Movatter.jp