정렬할 CategoricalIndex의 범주를 설정하려면 as_ordered()를 사용하세요. Pandas의 메소드
먼저 필요한 라이브러리를 가져옵니다 -
import pandas as pd
"categories" 매개변수를 사용하여 범주에 대한 범주를 설정합니다. −
catIndex = pd.CategoricalIndex(["p", "q", "r", "s","p", "q", "r", "s"], categories=["p", "q", "r", "s"])
카테고리 가져오기 -
print("\nDisplaying Categories from CategoricalIndex...\n",catIndex.categories)
주문할 카테고리 설정 -
print("\nCategoricalIndex ordered...\n", catIndex.as_ordered()) 예시
다음은 코드입니다 -
import pandas as pd
# Set the categories for the categorical using the "categories" parameter
catIndex = pd.CategoricalIndex(["p", "q", "r", "s","p", "q", "r", "s"], categories=["p", "q", "r", "s"])
# Display the CategoricalIndex
print("CategoricalIndex...\n",catIndex)
# Get the categories
print("\nDisplaying Categories from CategoricalIndex...\n",catIndex.categories)
# Set the categories to be ordered
print("\nCategoricalIndex ordered...\n", catIndex.as_ordered()) 출력
이것은 다음과 같은 출력을 생성합니다 -
CategoricalIndex... CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=False, dtype='category') Displaying Categories from CategoricalIndex... Index(['p', 'q', 'r', 's'], dtype='object') CategoricalIndex ordered... CategoricalIndex(['p', 'q', 'r', 's', 'p', 'q', 'r', 's'], categories=['p', 'q', 'r', 's'], ordered=True, dtype='category')