pandas.MultiIndex.get_level_values

MultiIndex.get_level_values(level)[source]

Return vector of label values for requested level, equal to the length of the index.

Parameters:

level : int or str

level is either the integer position of the level in the MultiIndex, or the name of the level.

Returns:

values : Index

values is a level of this MultiIndex converted to a single Index (or subclass thereof).

Examples

Create a MultiIndex:

>>> mi = pd.MultiIndex.from_arrays((list('abc'), list('def')))
>>> mi.names = ['level_1', 'level_2']

Get level values by supplying level as either integer or name:

>>> mi.get_level_values(0)
Index(['a', 'b', 'c'], dtype='object', name='level_1')
>>> mi.get_level_values('level_2')
Index(['d', 'e', 'f'], dtype='object', name='level_2')
Scroll To Top