카테고리에 순서 관계가 있는지 확인하려면 순서화된 CategoricalIndex의 속성입니다.
먼저 필요한 라이브러리를 가져옵니다 -
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("\nDisplayingCategories from CategoricalIndex...\n",catIndex.categories) 순서 관계에 대한 범주 확인 -
print("\nDoes categories have ordered relationship...\n",catIndex.ordered)
예
다음은 코드입니다 -
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("\nDisplayingCategories from CategoricalIndex...\n",catIndex.categories)
# Check categories for ordered relationship
print("\nDoes categories have ordered relationship...\n",catIndex.ordered) 출력
이것은 다음과 같은 출력을 생성합니다 -
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') Does categories have ordered relationship... True