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

Python Pandas - 고유하지 않은 값의 개체에 대해서도 새 인덱스에 대한 인덱서 및 마스크 계산

<시간/>

고유하지 않은 값 개체에 대해서도 새 인덱스에 대한 인덱서 및 마스크를 계산하려면 index.get_indexer_non_unique()를 사용하세요. method.Python Pandas - 고유하지 않은 값의 개체에 대해서도 새 인덱스에 대한 인덱서 및 마스크 계산

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

import pandas as pd

일부 고유하지 않은 값으로 Pandas 인덱스 생성 -

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

판다 인덱스 표시 -

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

인덱서와 마스크를 계산합니다. 인덱스에 없기 때문에 -1로 표시됩니다. 이것은 또한 고유하지 않은 인덱스 개체 값을 계산합니다 -

print("\nGet the indexes...\n",index.get_indexer_non_unique([30, 40, 90, 100, 50, 60]))

예시

다음은 코드입니다 -

import pandas as pd

# Creating Pandas index with some non-unique values
index = pd.Index([10, 20, 30, 40, 40, 50, 60, 60, 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
# Marked by -1, as it is not in index
# This also computes non-unique Index object values
print("\nGet the indexes...\n",index.get_indexer_non_unique([30, 40, 90, 100, 50, 60]))

출력

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

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

Number of elements in the index...
10

Get the indexes...
(array([ 2, 3, 4, -1, -1, 5, 6, 7, 8], dtype=int64), array([2, 3], dtype=int64))