MultiIndex에서 각 레벨 길이의 튜플을 얻으려면 MultiIndex.levshape를 사용하세요. Pandas의 속성
먼저 필요한 라이브러리를 가져옵니다 -
import pandas as pd
MultiIndex는 pandas 개체에 대한 다단계 또는 계층적 인덱스 개체입니다. 배열 생성 -
arrays = [[1, 2, 3, 4, 5], ['John', 'Tim', 'Jacob', 'Chris', 'Keiron']]
"names" 매개변수는 각 인덱스 수준의 이름을 설정합니다. from_arrays() 다중 인덱스를 만드는 데 사용되는 UI -
multiIndex = pd.MultiIndex.from_arrays(arrays, names=('ranks', 'student')) 각 레벨의 길이를 가진 튜플 얻기 -
print("\nThe tuple with the length of each level in a Multi-index...\n",multiIndex.levshape) 예시
다음은 코드입니다 -
import pandas as pd
# MultiIndex is a multi-level, or hierarchical, index object for pandas objects
# Create arrays
arrays = [[1, 2, 3, 4, 5], ['John', 'Tim', 'Jacob', 'Chris', 'Keiron']]
# The "names" parameter sets the names for each of the index levels
# The from_arrays() uis used to create a Multiindex
multiIndex = pd.MultiIndex.from_arrays(arrays, names=('ranks', 'student'))
# display the Multiindex
print("The Multi-index...\n",multiIndex)
# get the integer number of levels in Multiindex
print("\nThe number of levels in Multi-index...\n",multiIndex.nlevels)
# get the levels in Multiindex
print("\nThe levels in Multi-index...\n",multiIndex.levels)
# get a tuple with the length of each level
print("\nThe tuple with the length of each level in a Multi-index...\n",multiIndex.levshape) 출력
이것은 다음과 같은 출력을 생성합니다 -
The Multi-index...
MultiIndex([(1, 'John'),
(2, 'Tim'),
(3, 'Jacob'),
(4, 'Chris'),
(5, 'Keiron')],
names=['ranks', 'student'])
The number of levels in Multi-index...
2
The levels in Multi-index...
[[1, 2, 3, 4, 5], ['Chris', 'Jacob', 'John', 'Keiron', 'Tim']]
The tuple with the length of each level in a Multi-index...
(5, 5)