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

Python Pandas - 인덱서를 계산하고 정확히 일치하지 않는 경우 이전 인덱스 값을 찾습니다.

<시간/>

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

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

import pandas as pd

팬더 인덱스 생성 -

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

판다 인덱스 표시 -

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

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

print("\nGet the indexes...\n",index.get_indexer([30, 20, 75, 80, 50, 59], method="ffill"))

예시

다음은 코드입니다 -

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 previous index value if no exact match using the "method" parameter.
# The value is set "ffill"
print("\nGet the indexes...\n",index.get_indexer([30, 20, 75, 80, 50, 59], method="ffill"))

출력

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

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

Number of elements in the index...
7

Get the indexes...
[2 1 6 6 4 4]