pandas.MultiIndex.set_codes

MultiIndex.set_codes(codes, level=None, inplace=False, verify_integrity=True)[source]

Set new codes on MultiIndex. Defaults to returning new index.

New in version 0.24.0: New name for deprecated method set_labels.

Parameters:
codes : sequence or list of sequence

new codes to apply

level : int, level name, or sequence of int/level names (default None)

level(s) to set (None for all levels)

inplace : bool

if True, mutates in place

verify_integrity : bool (default True)

if True, checks that levels and codes are compatible

Returns:
new index (of same type and class…etc)

Examples

>>> idx = pd.MultiIndex.from_tuples([(1, u'one'), (1, u'two'),
                                    (2, u'one'), (2, u'two')],
                                    names=['foo', 'bar'])
>>> idx.set_codes([[1,0,1,0], [0,0,1,1]])
MultiIndex(levels=[[1, 2], [u'one', u'two']],
           labels=[[1, 0, 1, 0], [0, 0, 1, 1]],
           names=[u'foo', u'bar'])
>>> idx.set_codes([1,0,1,0], level=0)
MultiIndex(levels=[[1, 2], [u'one', u'two']],
           labels=[[1, 0, 1, 0], [0, 1, 0, 1]],
           names=[u'foo', u'bar'])
>>> idx.set_codes([0,0,1,1], level='bar')
MultiIndex(levels=[[1, 2], [u'one', u'two']],
           labels=[[0, 0, 1, 1], [0, 0, 1, 1]],
           names=[u'foo', u'bar'])
>>> idx.set_codes([[1,0,1,0], [0,0,1,1]], level=[0,1])
MultiIndex(levels=[[1, 2], [u'one', u'two']],
           labels=[[1, 0, 1, 0], [0, 0, 1, 1]],
           names=[u'foo', u'bar'])
Scroll To Top