pandas 1.4.2

ParametersReturnsBackRef
set_names(self, names, level=None, inplace: 'bool' = False)

Able to set new names partially and by level.

Parameters

names : label or list of label or dict-like for MultiIndex

Name(s) to set.

versionchanged
level : int, label or list of int or label, optional

If the index is a MultiIndex and names is not dict-like, level(s) to set (None for all levels). Otherwise level must be None.

versionchanged
inplace : bool, default False

Modifies the object directly, instead of creating a new Index or MultiIndex.

Returns

Index or None

The same type as the caller or None if inplace=True .

Set Index or MultiIndex name.

See Also

Index.rename

Able to set new names without level.

Examples

This example is valid syntax, but we were not able to check execution
>>> idx = pd.Index([1, 2, 3, 4])
... idx Int64Index([1, 2, 3, 4], dtype='int64')
This example is valid syntax, but we were not able to check execution
>>> idx.set_names('quarter')
Int64Index([1, 2, 3, 4], dtype='int64', name='quarter')
This example is valid syntax, but we were not able to check execution
>>> idx = pd.MultiIndex.from_product([['python', 'cobra'],
...  [2018, 2019]])
... idx MultiIndex([('python', 2018), ('python', 2019), ( 'cobra', 2018), ( 'cobra', 2019)], )
This example is valid syntax, but we were not able to check execution
>>> idx.set_names(['kind', 'year'], inplace=True)
... idx MultiIndex([('python', 2018), ('python', 2019), ( 'cobra', 2018), ( 'cobra', 2019)], names=['kind', 'year'])
This example is valid syntax, but we were not able to check execution
>>> idx.set_names('species', level=0)
MultiIndex([('python', 2018),
            ('python', 2019),
            ( 'cobra', 2018),
            ( 'cobra', 2019)],
           names=['species', 'year'])

When renaming levels with a dict, levels can not be passed.

This example is valid syntax, but we were not able to check execution
>>> idx.set_names({'kind': 'snake'})
MultiIndex([('python', 2018),
            ('python', 2019),
            ( 'cobra', 2018),
            ( 'cobra', 2019)],
           names=['snake', 'year'])
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.core.indexes.base.Index.rename

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/indexes/base.py#1733
type: <class 'function'>
Commit: