Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.CategoricalIndex.map#

CategoricalIndex.map(mapper,na_action=None)[source]#

Map values using input an input mapping or function.

Maps the values (their categories, not the codes) of the index to newcategories. If the mapping correspondence is one-to-one the result is aCategoricalIndex which has the same order property asthe original, otherwise anIndex is returned.

If adict orSeries is used any unmapped category ismapped toNaN. Note that if this happens anIndexwill be returned.

Parameters:
mapperfunction, dict, or Series

Mapping correspondence.

Returns:
pandas.CategoricalIndex or pandas.Index

Mapped index.

See also

Index.map

Apply a mapping correspondence on anIndex.

Series.map

Apply a mapping correspondence on aSeries.

Series.apply

Apply more complex functions on aSeries.

Examples

>>>idx=pd.CategoricalIndex(['a','b','c'])>>>idxCategoricalIndex(['a', 'b', 'c'], categories=['a', 'b', 'c'],                  ordered=False, dtype='category')>>>idx.map(lambdax:x.upper())CategoricalIndex(['A', 'B', 'C'], categories=['A', 'B', 'C'],                 ordered=False, dtype='category')>>>idx.map({'a':'first','b':'second','c':'third'})CategoricalIndex(['first', 'second', 'third'], categories=['first',                 'second', 'third'], ordered=False, dtype='category')

If the mapping is one-to-one the ordering of the categories ispreserved:

>>>idx=pd.CategoricalIndex(['a','b','c'],ordered=True)>>>idxCategoricalIndex(['a', 'b', 'c'], categories=['a', 'b', 'c'],                 ordered=True, dtype='category')>>>idx.map({'a':3,'b':2,'c':1})CategoricalIndex([3, 2, 1], categories=[3, 2, 1], ordered=True,                 dtype='category')

If the mapping is not one-to-one anIndex is returned:

>>>idx.map({'a':'first','b':'second','c':'first'})Index(['first', 'second', 'first'], dtype='object')

If adict is used, all unmapped categories are mapped toNaN andthe result is anIndex:

>>>idx.map({'a':'first','b':'second'})Index(['first', 'second', nan], dtype='object')

[8]ページ先頭

©2009-2025 Movatter.jp