Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python Pandas - 입력 레이블의 슬라이스 위치 계산

<시간/>

입력 레이블의 슬라이스 위치를 계산하려면 index.slice_locs()를 사용하세요. 방법. 먼저 필요한 라이브러리를 가져옵니다 -

import pandas as pd

Pandas 인덱스 객체 생성 -

index = pd.Index(list('pqrstuvwxyz'))

판다 인덱스 표시 -

print("Pandas Index...\n",index)

슬라이스 위치를 가져옵니다. "start"는 시작할 레이블입니다. "end"는 끝나는 레이블입니다.

print("\nThe slice locations with start and stop...\n",index.slice_locs(start='q', end='v'))

다음은 코드입니다 -

import pandas as pd

# Create Pandas index object
index = pd.Index(list('pqrstuvwxyz'))

# Display the Pandas index
print("Pandas Index...\n",index)

# Return the number of elements in the Index
print("\nNumber of elements in the index...\n",index.size)

# Get the slice locations
# The "start" is the label to begin with
# The "end" is the label to end with
print("\nThe slice locations with start and stop...\n",index.slice_locs(start='q', end='v'))

출력

이것은 다음과 같은 출력을 생성합니다 -

Pandas Index...
Index(['p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'], dtype='object')

Number of elements in the index...
11

The slice locations with start and stop...
(1, 7)