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

Python - Pandas Index에서 순서를 유지하기 위해 요소를 삽입해야 하는 인덱스 찾기

<시간/>

Pandas Index에서 순서를 유지하기 위해 요소를 삽입해야 하는 인덱스를 찾으려면 index.searchsorted()를 사용하세요. 방법.

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

import pandas as pd

판다 인덱스 생성 -

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

판다 인덱스 표시 -

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

Searchsorted:삽입할 값을 설정하고 그것이 배치되어야 하는 정확한 인덱스 위치를 얻습니다 -

print("\nThe exact positions where the element should be placed?...\n",index.searchsorted(45))

예시

다음은 코드입니다 -

import pandas as pd

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

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

# searchsorted
# set the value to insert and get the exact index position where it should be placed
print("\nThe exact positions where the element should be placed?...\n",index.searchsorted(45))

출력

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

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

Number of elements in the index...
5

The exact positions where the element should be placed?...
4