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

Python Pandas - 인덱서를 계산하고 정확히 일치하지 않으면 다음 인덱스 값을 찾습니다.

<시간/>

인덱서를 계산하고 정확히 일치하지 않는 경우 다음 인덱스 값을 찾으려면 index.get_indexer()를 사용하세요. 방법. 방법도 설정 bfill 매개변수 .

먼저 필요한 라이브러리를 가져옵니다 -

import pandas as pd

판다 인덱스 생성 -

index = pd.Index([10, 20, 30, 40, 50, 60, 70])

팬더 인덱스 표시 -

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

"get_indexer"를 사용하여 인덱서 및 마스크를 계산합니다. "method" 매개변수를 사용하여 정확히 일치하지 않는 경우 다음 인덱스 값을 찾습니다. 값은 "bfill"로 설정됩니다 -

print("\nGet the indexes...\n",index.get_indexer([30, 25, 58, 50, 55], method="bfill"))

예시

다음은 코드입니다 -

import pandas as pd

# Creating Pandas index
index = pd.Index([10, 20, 30, 40, 50, 60, 70])

# 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)

# Compute indexer and mask using the "get_indexer"
# Find the next index value if no exact match using the "method" parameter.
# The value is set "bfill"
print("\nGet the indexes...\n",index.get_indexer([30, 25, 58, 50, 55], method="bfill"))

출력

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

Pandas Index...
Int64Index([10, 20, 30, 40, 50, 60, 70], dtype='int64')

Number of elements in the index...
7

Get the indexes...
[2 2 5 4 5]