MultiIndex에서 레이블 시퀀스의 위치를 얻으려면 MutiIndex.get_locs()를 사용하세요. Pandas의 메소드
먼저 필요한 라이브러리를 가져옵니다 -
import pandas as pd
MultiIndex는 pandas 개체에 대한 다단계 또는 계층적 인덱스 개체입니다.
multiIndex = pd.MultiIndex.from_arrays([list('pqrrst'), list('kytssp')])
MultiIndex 표시 -
print("The MultiIndex...\n",multiIndex) 일련의 레이블 위치 가져오기 -
print("\nGet the locations in MultiIndex...\n",multiIndex.get_locs('s'))
예시
다음은 코드입니다 -
import pandas as pd
# MultiIndex is a multi-level, or hierarchical, index object for pandas objects
multiIndex = pd.MultiIndex.from_arrays([list('pqrrst'), list('kytssp')])
# display the MultiIndex
print("The MultiIndex...\n",multiIndex)
# get the levels in MultiIndex
print("\nThe levels in MultiIndex...\n",multiIndex.levels)
# Get the location for a sequence of labels
print("\nGet the locations in MultiIndex...\n",multiIndex.get_locs('s')) 출력
이것은 다음과 같은 출력을 생성합니다 -
The MultiIndex...
MultiIndex([('p', 'k'),
('q', 'y'),
('r', 't'),
('r', 's'),
('s', 's'),
('t', 'p')],
)
The levels in MultiIndex...
[['p', 'q', 'r', 's', 't'], ['k', 'p', 's', 't', 'y']]
Get the locations in MultiIndex...
[4]