- API reference
- pandas.MultiIndex
- pandas.Multi...
pandas.MultiIndex.get_level_values#
- MultiIndex.get_level_values(level)[source]#
Return vector of label values for requested level.
Length of returned vector is equal to the length of the index.
- Parameters:
- levelint or str
levelis either the integer position of the level in theMultiIndex, or the name of the level.
- Returns:
- Index
Values is a level of this MultiIndex converted toa single
Index(or subclass thereof).
Notes
If the level contains missing values, the result may be casted to
floatwith missing values specified asNaN. This is becausethe level is converted to a regularIndex.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')
If a level contains missing values, the return type of the levelmay be cast to
float.>>>pd.MultiIndex.from_arrays([[1,None,2],[3,4,5]]).dtypeslevel_0 int64level_1 int64dtype: object>>>pd.MultiIndex.from_arrays([[1,None,2],[3,4,5]]).get_level_values(0)Index([1.0, nan, 2.0], dtype='float64')
On this page