Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.MultiIndex.levels#

MultiIndex.levels[source]#

Levels of the MultiIndex.

Levels refer to the different hierarchical levels or layers in a MultiIndex.In a MultiIndex, each level represents a distinct dimension or category ofthe index.

To access the levels, you can use the levels attribute of the MultiIndex,which returns a tuple of Index objects. Each Index object represents alevel in the MultiIndex and contains the unique values found in thatspecific level.

If a MultiIndex is created with levels A, B, C, and the DataFrame usingit filters out all rows of the level C, MultiIndex.levels will stillreturn A, B, C.

Examples

>>>index=pd.MultiIndex.from_product([['mammal'],...('goat','human','cat','dog')],...names=['Category','Animals'])>>>leg_num=pd.DataFrame(data=(4,2,4,4),index=index,columns=['Legs'])>>>leg_num                  LegsCategory Animalsmammal   goat        4         human       2         cat         4         dog         4
>>>leg_num.index.levelsFrozenList([['mammal'], ['cat', 'dog', 'goat', 'human']])

MultiIndex levels will not change even if the DataFrame using the MultiIndexdoes not contain all them anymore.See how “human” is not in the DataFrame, but it is still in levels:

>>>large_leg_num=leg_num[leg_num.Legs>2]>>>large_leg_num                  LegsCategory Animalsmammal   goat        4         cat         4         dog         4
>>>large_leg_num.index.levelsFrozenList([['mammal'], ['cat', 'dog', 'goat', 'human']])

[8]ページ先頭

©2009-2025 Movatter.jp