pyspark.pandas.Index.rename#

Index.rename(name, inplace=False)[source]#

Alter Index or MultiIndex name. Able to set new names without level. Defaults to returning a new index.

Parameters
namelabel or list of labels

Name(s) to set.

inplaceboolean, default False

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

Returns
Index or MultiIndex

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

Examples

>>> df = ps.DataFrame({'a': ['A', 'C'], 'b': ['A', 'B']}, columns=['a', 'b'])
>>> df.index.rename("c")
Index([0, 1], dtype='int64', name='c')
>>> df.set_index("a", inplace=True)
>>> df.index.rename("d")
Index(['A', 'C'], dtype='object', name='d')

You can also change the index name in place.

>>> df.index.rename("e", inplace=True)
>>> df.index
Index(['A', 'C'], dtype='object', name='e')
>>> df  
   b
e
A  A
C  B

Support for MultiIndex

>>> psidx = ps.MultiIndex.from_tuples([('a', 'x'), ('b', 'y')])
>>> psidx.names = ['hello', 'pandas-on-Spark']
>>> psidx  
MultiIndex([('a', 'x'),
            ('b', 'y')],
           names=['hello', 'pandas-on-Spark'])
>>> psidx.rename(['aloha', 'databricks'])  
MultiIndex([('a', 'x'),
            ('b', 'y')],
           names=['aloha', 'databricks'])