pyspark.pandas.Index.set_names

Index.set_names(names: Union[Any, Tuple[Any, …], List[Union[Any, Tuple[Any, …]]]], level: Union[int, Any, Tuple[Any, …], List[Union[int, Any, Tuple[Any, …]]], None] = None, inplace: bool = False) → Optional[pyspark.pandas.indexes.base.Index][source]

Set Index or MultiIndex name. Able to set new names partially and by level.

Parameters
nameslabel or list of label

Name(s) to set.

levelint, label or list of int or label, optional

If the index is a MultiIndex, level(s) to set (None for all levels). Otherwise level must be None.

inplacebool, default False

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

Returns
Index

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

See also

Index.rename

Able to set new names without level.

Examples

>>> idx = ps.Index([1, 2, 3, 4])
>>> idx
Int64Index([1, 2, 3, 4], dtype='int64')
>>> idx.set_names('quarter')
Int64Index([1, 2, 3, 4], dtype='int64', name='quarter')

For MultiIndex

>>> idx = ps.MultiIndex.from_tuples([('a', 'x'), ('b', 'y')])
>>> idx  
MultiIndex([('a', 'x'),
            ('b', 'y')],
           )
>>> idx.set_names(['kind', 'year'], inplace=True)
>>> idx  
MultiIndex([('a', 'x'),
            ('b', 'y')],
           names=['kind', 'year'])
>>> idx.set_names('species', level=0)  
MultiIndex([('a', 'x'),
            ('b', 'y')],
           names=['species', 'year'])