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

Python Pandas - Ordered CategoricalIndex에서 최소값 가져오기

<시간/>

Ordered CategoricalIndex에서 최소값을 얻으려면 catIndex.min()을 사용하세요. Pandas의 메소드. 먼저 필요한 라이브러리를 가져옵니다 -

import pandas as pd

"categories" 매개변수를 사용하여 범주에 대한 범주를 설정합니다. "ordered" 매개변수를 사용하여 범주형을 정렬된 것으로 취급하십시오 -

catIndex = pd.CategoricalIndex(
   ["p", "q", "r", "s","p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"]
)

범주형 인덱스 표시 -

print("Categorical Index...\n",catIndex)

최소값 가져오기 -

print("\nMinimum value from CategoricalIndex...\n",catIndex.min())

예시

다음은 코드입니다 -

import pandas as pd

# CategoricalIndex is the Index based on an underlying Categorical
# Set the categories for the categorical using the "categories" parameter
# Treat the categorical as ordered using the "ordered" parameter
catIndex = pd.CategoricalIndex(
   ["p", "q", "r", "s","p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"]
)

# Display the Categorical Index
print("Categorical Index...\n",catIndex)

# Get the categories
print("\nDisplayingCategories from CategoricalIndex...\n",catIndex.categories)

# Get the min value
print("\nMinimum value from CategoricalIndex...\n",catIndex.min())

출력

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

Categorical Index...
CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=True, dtype='category')

DisplayingCategories from CategoricalIndex...
Index(['p', 'q', 'r', 's'], dtype='object')

Minimum value from CategoricalIndex...
P