Participer au site avec un Tip
Rechercher
 

Améliorations / Corrections

Vous avez des améliorations (ou des corrections) à proposer pour ce document : je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.

Emplacement :

Description des améliorations :

Vous êtes un professionnel et vous avez besoin d'une formation ? Programmation Python
Les fondamentaux
Voir le programme détaillé
Classe « Categorical »

Méthode pandas.Categorical.set_categories

Signature de la méthode set_categories

def set_categories(self, new_categories, ordered=None, rename: 'bool' = False) 

Description

help(Categorical.set_categories)

Set the categories to the specified new categories.

``new_categories`` can include new categories (which will result in
unused categories) or remove old categories (which results in values
set to ``NaN``). If ``rename=True``, the categories will simply be renamed
(less or more items than in old categories will result in values set to
``NaN`` or in unused categories respectively).

This method can be used to perform more than one action of adding,
removing, and reordering simultaneously and is therefore faster than
performing the individual steps via the more specialised methods.

On the other hand this methods does not do checks (e.g., whether the
old categories are included in the new categories on a reorder), which
can result in surprising changes, for example when using special string
dtypes, which does not considers a S1 string equal to a single char
python string.

Parameters
----------
new_categories : Index-like
   The categories in new order.
ordered : bool, default False
   Whether or not the categorical is treated as a ordered categorical.
   If not given, do not change the ordered information.
rename : bool, default False
   Whether or not the new_categories should be considered as a rename
   of the old categories or as reordered categories.

Returns
-------
Categorical with reordered categories.

Raises
------
ValueError
    If new_categories does not validate as categories

See Also
--------
rename_categories : Rename categories.
reorder_categories : Reorder categories.
add_categories : Add new categories.
remove_categories : Remove the specified categories.
remove_unused_categories : Remove categories which are not used.

Examples
--------
For :class:`pandas.Series`:

>>> raw_cat = pd.Categorical(['a', 'b', 'c', 'A'],
...                           categories=['a', 'b', 'c'], ordered=True)
>>> ser = pd.Series(raw_cat)
>>> ser
0   a
1   b
2   c
3   NaN
dtype: category
Categories (3, object): ['a' < 'b' < 'c']

>>> ser.cat.set_categories(['A', 'B', 'C'], rename=True)
0   A
1   B
2   C
3   NaN
dtype: category
Categories (3, object): ['A' < 'B' < 'C']

For :class:`pandas.CategoricalIndex`:

>>> ci = pd.CategoricalIndex(['a', 'b', 'c', 'A'],
...                          categories=['a', 'b', 'c'], ordered=True)
>>> ci
CategoricalIndex(['a', 'b', 'c', nan], categories=['a', 'b', 'c'],
                 ordered=True, dtype='category')

>>> ci.set_categories(['A', 'b', 'c'])
CategoricalIndex([nan, 'b', 'c', nan], categories=['A', 'b', 'c'],
                 ordered=True, dtype='category')
>>> ci.set_categories(['A', 'b', 'c'], rename=True)
CategoricalIndex(['A', 'b', 'c', nan], categories=['A', 'b', 'c'],
                 ordered=True, dtype='category')


Vous êtes un professionnel et vous avez besoin d'une formation ? RAG (Retrieval-Augmented Generation)
et Fine Tuning d'un LLM
Voir le programme détaillé