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

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

<시간/>

Ordered CategoricalIndex에서 최대값을 얻으려면 Pandas에서 catIndex.max() 메서드를 사용하세요.

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

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("\nMaximum value from CategoricalIndex...\n",catIndex.max())

예시

다음은 코드입니다 -

import pandas as pd

# CategoricalIndex can only take on a limited, and usually fixed, number of possible values.
# 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("\nDisplaying Categories from CategoricalIndex...\n",catIndex.categories)

# Get the max value
print("\nMaximum value from CategoricalIndex...\n",catIndex.max())

출력

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

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

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

Maximum value from CategoricalIndex...
S
의 최대값